1. array.join 배열을 문자열로 변환
{
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join(' and ');
console.log(result);
}
2. array.split 문자열을 배열로 변환
{
const fruits = '🍎, 🥝, 🍌, 🍒';
const result = fruits.split(',', 3);
console.log(result);
}
3. array.reverse
{
const array = [1, 2, 3, 4, 5];
const result = array.reverse();
console.log(result);
console.log(array);
}
4. array.slice 배열의 특정 부분 리턴
{
const array = [1, 2, 3, 4, 5];
const result = array.slice(2,5);
console.log(result);
}
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),
];
5. array.find
- 점수가 90점 이상인 학생 찾기
- 콜백함수는 boolean 타입을 리턴한다. 최초 true 값 리턴.
{
const result = students.find((student)=> student.score === 90);
console.log(result);
6. array.filter
{
const result = students.filter((student)=> student.enrolled);
console.log(result);
}
7. array.map
{
const result = students.map((student)=> student.score);
}
8. array.some, array.every
{
const result = students.some((student) => student.score < 50);
console.log(result);
const result2 = !students.every((student) => student.score > 50);
console.log(result2);
}
9. array.reduce
- 점수의 평균값 구하기
- reduce는 우리가 원하는 시작점부터 배열에 있는 모든 값을 누적하는 API
{
const result = students.reduce((prev, curr) => {
console.log('---------------------');
console.log(prev);
console.log(curr);
return prev + curr.score;
}, 0);
console.log(result/students.length);
}
10. API 묶어서 이용하기
- 50점 이상인 점수를 모두 포함한 문자열 만들기
{
const result = students
.map((student) => student.score)
.filter((score) => score >= 50)
.join();
console.log(result);
}
11. array.sort
{
const result = students.map(student => student.score)
.sort((a, b) => a - b)
.join();
console.log(result);
}