[JavaScript] 9. Array API
1. join
- make a string out of an array.
- join(separator?: string): string;
'use strict';
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join(',');
console.log(result); // apple, banana, orange
2. split
- make an array out of a string.
- split(separator: string | RegExp, limit?: number): string[];
const fruits = 'apple, banana, orange';
const result1 = fruits.split(',');
console.log(result1); // ["apple", " banana", " orange"]
const result1 = fruits.split(',', 2);
console.log(result2); // ["apple", " banana"]
3. reverse
- make this array look like this: [5, 4, 3, 2, 1]
const arr = [1, 2, 3, 4, 5];
const result = arr.reverse();
console.log(result); // [5, 4, 3, 2, 1]
console.log(arr); // [5, 4, 3, 2, 1]
4. slice
- make new array without the first two elements.
- slice(start?: number, end?: number): T[];
const arr = [1, 2, 3, 4, 5];
const result = arr.slice(2, 5);
console.log(result); // [3, 4, 5]
console.log(arr); // [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),
];
5. find
- Returns the value of the first element in the array where predicate is true, and undefined.
// find a student with the score 90.
const result = students.find(function(student, index) {
return student.score === 90;
}); // students.find((student) => student.score === 90);로 생략 가능
console.log(result); // Student {name: "C", age: 30, enrolled: true, score: 90}
6. filter
- Returns the elements of an array that meet the condition specified in a callback function.
const result = students.filter((student) => student.enrolled);
console.log(result);
7. map
- Calls a defined callback function on each element of an array.
const result = students.map((student) => student.score*2);
console.log(result); // [90, 160, 180, 132, 176]
8. some & every
- Determines whether the specified callback function returns true.
// check if there is a student with the score lower than 50.
const result = students.some((student) => student.score < 50);
console.log(result); // true
result = !students.every((student) => student.score >= 50);
console.log(result); // true
9. reduce
- Calls the specified callback function for all the elements in an array.
const result = students.reduce((prev, curr) => prev + curr.score, 0);
console.log(result / students.length); // 73.8
Quiz1. make a string containing all the scores bigger than 50.
- result should be: '80,90,66,88'
const result = students
.map((student) => student.score)
.filter((score) => score >= 50)
.join();
console.log(result); // 80,90,66,88
Quiz2. do 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); // 45,80,90,66,88