728x90
9. 유용한 10가지 배열 함수들. Array APIs 총정리 | 프론트엔드 개발자 입문편 ( JavaScript ES6)
// Q1. make a string out of an array
{
const fruits = ["apple", "banana", "orange"];
// di it yourself!
// console.log(fruits.toString()); // .join과 .toString의 차이는 구분자(seperator)
// answer
const result = fruits.join(" and ");
console.log(result);
}
// Q2. make an array out of a string
{
const fruits = "🍎, 🥝, 🍌, 🍒";
// do it yourself!
// const fruit = fruits.split(",");
// console.log(fruit);
// answer
const fruit = fruits.split(",");
console.log(fruit);
}
// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
const array = [1, 2, 3, 4, 5];
// do it yourself!
// const reverseArray = array.reverse();
// console.log(reverseArray);
// answer
const reverseArray = array.reverse();
console.log(reverseArray);
console.log(array); // 원래 배열도 바뀐다.
}
// Q4. make new array without the first two elements
{
const array = [1, 2, 3, 4, 5];
// do it yourself!
// const result = array.slice(2);
// const resultNew = array.slice(2, 5);
// console.log(result);
// console.log(resultNew);
// // answer
const result = array.slice(2, 5);
console.log(result);
console.log(array);
}
// -------------------------------------------------------------
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
{
// do it yourself!
// for (let i = 0; i < students.length; i++) {
// if (students[i].score === 90) {
// console.log(students[i]);
// }
// }
// answer
// const result = students.find(function (value) {
// return value.score === 90;
// });
const result = students.find((value) => value.score === 90);
console.log(result);
}
// Q6. make an array of enrolled students
{
// do it yourself!
// const result = students.filter((value) => value.enrolled === true);
// console.log(result);
// // answer
const result = students.filter((student) => student.enrolled === true);
console.log(result);
}
// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
// do it yourself!
// let result = [];
// for (let i = 0; i < students.length; i++) {
// result.push(students[i].score);
// }
// console.log(result);
// answer
const result = students.map((student) => student.score);
console.log(result);
}
// Q8. check if there is a student with the score lower than 50
{
// do it yourself!
// const result = students.some((student) => student.score < 50);
// console.log(result);
// answer
const result = students.some((student) => student.score < 50);
console.log(result);
const result2 = !students.every((student) => student.score >= 50);
console.log(result2);
}
// Q9. compute students' average score
{
//do it yourself!
// let result = 0;
// for (let i = 0; i < students.length; i++) {
// result += students[i].score;
// }
// console.log(result / 5);
// answer
// const result = students.reduce((prev, curr) => {
// console.log("----------");
// console.log(prev);
// console.log(curr);
// return prev + curr.score;
// }, 0);
const result = students.reduce((prev, curr) => 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'
{
// do it yourself!
// const result = students.map((student) => student.score);
// const resultArray = result.join();
// console.log(resultArray);
// answer
const result = students
.map((student) => student.score)
// .filter((score) => score >= 50)
.join();
console.log(result);
}
// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
// do it yourself!
// const result = students
// .map((student) => student.score)
// .sort((a, b) => a - b)
// .join();
// console.log(result);
// answer
const result = students
.map((student) => student.score)
.sort((a, b) => a - b)
.join();
console.log(result);
}
반응형
'Programming > JavaScript' 카테고리의 다른 글
[Javascript][Lecture][드림코딩]자바스크립트 기초 강의(ES5+)(11/13) (0) | 2021.11.10 |
---|---|
[Javascript][Lecture][드림코딩]자바스크립트 기초 강의(ES5+)(10/13) (0) | 2021.11.10 |
[Javascript][Lecture][드림코딩]자바스크립트 기초 강의(ES5+)(8/13) (0) | 2021.11.08 |
[Javascript]객체와 객체의 속성(property) 정의 (0) | 2021.11.08 |
[Javascript][Lecture][드림코딩]자바스크립트 기초 강의(ES5+)(7/13) (0) | 2021.11.07 |