arr.join([separator?]) // ?는 매개변수를 있어도 없어도 상관없다는 표시!!
const fruits = ['apple', 'banana', 'orange']; const String1 = fruits.join(); // apple,banana,orange const String2 = fruits.join(" + "); // apple + banana + orange const String3 = fruits.join(" | "); //apple | banana | orange const String4 = fruits.join(" and "); // apple and banana and orange
split(separator, limit?);
const fruits = '🍎, 🥝, 🍌, 🍒'; const array1 = fruits.split(","); // (4) ['🍎', ' 🥝', ' 🍌', ' 🍒'] const array2 = fruits.split(",", 2); // (4) ['🍎', ' 🥝'] const array3 = fruits.split(); // (4) ['🍎', ' 🥝']
const array = [1, 2, 3, 4, 5]; const reverseArray = array.reverse(); console.log(reverseArray); // (5) [5, 4, 3, 2, 1] console.log(array); // reverse함수 사용 시 원래 배열의 순서도 바뀜 (5) [5, 4, 3, 2, 1]
arr.slice([begin[, end]])
const array = [1, 2, 3, 4, 5]; const result = array.slice(0, 2); console.log(result); // (2) [1, 2] console.log(array); // slice는 복사본을 리턴하기 떄문에 원본 변함없음(5) [1, 2, 3, 4, 5]
find(callbackFn, thisArg)
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), const result = students.find(students => students.score === 90); console.log(result); // 90인 학생을 리턴 Student {name: 'C', age: 30, enrolled: true, score: 90} ];
filter(callbackFn, thisArg)
//enrolled ture인 학생 const enrolledStudents = students.filter(student => student.enrolled); console.log(enrolledStudents);
map(callbackFn, thisArg)
const newobjScore = students.map(objScore => objScore.score); console.log(newobjScore); //[45, 80, 90, 66, 88]
- 배열 안의 어떤 요소라도 주어진 callbackFn의 조건이 적어도 하나라도 통과하는지 테스트합니다.
8.2. 매개변수
some(callbackFn, thisArg)
// 학생 중 50미만이 있나요? const result = students.some(studentScore => studentScore.score < 50); console.log(result);
arr.reduce(callback[, initialValue])
accumulator
누산기는 콜백의 반환값을 누적합니다. 콜백의 이전 반환값 또는, 콜백의 첫 번째 호출이면서 initialValue를 제공한 경우에는 initialValue의 값입니다.
currentValue
처리할 현재 요소.
currentIndex Optional
처리할 현재 요소의 인덱스. initialValue를 제공한 경우 0, 아니면 1부터 시작합니다.
array Optional
reduce()를 호출한 배열.
누적 계산의 결과 값.
const result = students.reduce((students1, students2) => { console.log("------------"); console.log(students1); console.log(students2); return students1 + students2.score; }, 0); console.log(result);
- 배열의 요소를 적절한 위치에 정렬한 후 그 배열을 반환합니다.
10.2. 매개변수
arr.sort([compareFunction]);
// Bonus! do Q10 sorted in ascending order // result should be: '45, 66, 80, 88, 90' { const result = students.map((student) => student.score) .sort((a, b) => a - b).join(); console.log(result); }
// Q1. make a string out of an array
{
const fruits = ['apple', 'banana', 'orange'];
const String1 = fruits.join(); // apple,banana,orange
const String2 = fruits.join(" + "); // apple + banana + orange
const String3 = fruits.join(" | "); //apple | banana | orange
const String4 = fruits.join(" and "); // apple and banana and orange
}
// Q2. make an array out of a string
{
const fruits = '🍎, 🥝, 🍌, 🍒';
const array1 = fruits.split(","); // (4) ['🍎', ' 🥝', ' 🍌', ' 🍒']
const array2 = fruits.split(",", 2); // (4) ['🍎', ' 🥝']
const array3 = fruits.split(); // (4) ['🍎', ' 🥝']
}
// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
const array = [1, 2, 3, 4, 5];
const reverseArray = array.reverse();
console.log(reverseArray); // (5) [5, 4, 3, 2, 1]
console.log(array); // array가 리턴으므로 reverse함수 사용 시 원래 배열의 순서도 바뀜 (5) [5, 4, 3, 2, 1]
}
// Q4. make new array without the first two elements
{
const array = [1, 2, 3, 4, 5];
const result = array.slice(0, 2);
console.log(result); // (2) [1, 2]
console.log(array); // slice는 복사본을 리턴하기 떄문에 원본 변함없음(5) [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(students => students.score === 90);
console.log(result); // 90인 학생을 리턴 Student {name: 'C', age: 30, enrolled: true, score: 90}
}
// Q6. make an array of enrolled students
{
//enrolled ture인 학생
const enrolledStudents = students.filter(student => student.enrolled);
console.log(enrolledStudents);
}
// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
const newobjScore = students.map(objScore => objScore.score);
console.log(newobjScore); //[45, 80, 90, 66, 88]
}
// Q8. check if there is a student with the score lower than 50
{
const result = students.some(studentScore => studentScore.score < 50);
console.log(result);
}
// Q9. compute students' average score
{
const result = students.reduce((students1, students2) => {
console.log("------------");
console.log(students1);
console.log(students2);
return students1 + students2.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((student) => student.score)
.join();
console.log(result);
}
// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
const result = students.map((student) => student.score)
.sort((a, b) => a - b).join();
console.log(result);
}