Study & Project ✏️/JavaScript 🐥

JS 배열 API 공부

JM 2021. 12. 27. 21:35
반응형

출처. https://www.youtube.com/watch?v=3CUjtKJ7PJg&t=122s 

API종류도 많고

Python의 map과 비슷한 개념과

.filter().join() 같이 두 번 연속으로 API를 쓰는 건 연습을 잘해야겠다.

sort에서 .sort((a,b) => a-b) 는 정말 익숙하지 않다 ㄷㄷ

// Q1. make a string out of an array
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join(',');
console.log(result);    //apple,banana,orange

// Q2. make an array out of a string
const fruits = 'apple, banana, cherry';
const result = fruits.split(',');
console.log(result);    //["apple","banana","orange"]
const result = fruits.split(',', 2);
console.log(result);    //["apple","banana"]

// Q3. make this array look like this: [5, 4, 3, 2, 1];
const array = [1, 2, 3, 4, 5];
const result = array.reverse();
console.log(result);    //[5, 4, 3, 2, 1]
console.log(array);    //[5, 4, 3, 2, 1] 함수를 reverse하고 return하는 것!

// Q4. make new arrray without the firtst two elements
const array = [1, 2, 3, 4, 5];
//배열을 변형시킴
const result = array.splice(0, 2);
console.log(result);    //[1, 2]
console.log(array);    //[3, 4, 5]
//배열을 변형x
const result = array.slice(2, 5);
console.log(result);    //[3, 4, 5]
console.log(array);    //[1, 2, 3, 4, 5]

//자료
class Student {
    constructor(name, age, enrolled, score) {
        this.name = name;
        this.age = age;
        this.enrolled = enrolled;
        this.score = score;
    }
}
const students = [
    new Student('A', 29, true, 45),
    new Student('B', 28, false, 80),
    new Student('C', 30, true, 90),
    new Student('D', 40, false, 66),
    new Student('E', 18, true, 88),
];

// Q5. find a student with the score 90
const result = students.find((student) => student.score === 90);
console.log(result);

// Q6. make an array of enrolled students
const result = students.filter((students) => students.enrolled);
console.log(result);

// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
const result = students.map((students) => students.score);
console.log(result);

// Q8. check if there is a student with the score lower than 50
const result = students.some((students) => students.score < 50);
console.log(result);
// All array[index] is true?
const result2 = students.every((students) => students.score < 50);
console.log(result2);

// Q9. conpute student's average score
// 앞에서부터 배열을 돌면서 값을 더할때 사용
// return값을 curr값으로 사용하기 때문
const result = students.reduce((prev, curr) => {
    console.log('--------');
    console.log(prev);
    console.log(curr);
    return prev + curr.score;
}, 0);
console.log(result / students.length);

// 뒤에서부터 돌기
const result = students.reduceRight((prev, curr) => {
    console.log('--------');
    console.log(prev);
    console.log(curr);
    return prev + curr.score;
}, 0);
console.log(result / students.length);

// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
const result = students.map((students) => students.score)
    .filter((score) => score >= 50)
    .join();
console.log(result);

// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
const result = students.map((students) => students.score)
    .sort((a, b) => a - b)
    .join();
console.log(result);

자주 쓰는 게 제일 좋을 듯!

'Study & Project ✏️ > JavaScript 🐥' 카테고리의 다른 글

JS callback 입문!  (0) 2021.12.28
JS JSON 입문!  (0) 2021.12.28
JS array 입문!  (0) 2021.12.25
JS Class 입문!  (0) 2021.12.25
JS Object 입문!  (0) 2021.12.24