메서드의 인수로 콜백함수를 받는 배열 고차함수에 대해 알아보자
-예시1-
const fruits = ['Banana', 'Orange', 'Apple']
fruits.sort();
console.log(fruits) // ["Apple", "Banana", "Orange"]
fruits.reverse()
console.log(fruits) // ["Orange", "Banana", "Apple"]
-예시2-
const points = [40, 100, 1, 5, 25, 10]
const ab = points.sort()
console.log(ab) // [1, 10, 100, 25, 40, 5]
-예시3-
const points = [40, 100, 1, 5, 25, 10]
const ab = points.sort()
console.log(ab) // [1, 10, 100, 25, 40, 5]
-예시4- 오름차순 정렬 : 비교함수의 반환값이 0보다 작으면 a를 우선하여 정렬한다
const points = [40, 100, 1, 5, 25, 10]
const abc = points.sort((a, b) => a - b)
console.log(abc) // [1, 10, 100, 25, 40, 5]
-예시5- 내림차순 정렬 : 비교함수의 반환값이 0보다 작으면 b를 우선하여 정렬한다
const points1 = [40, 100, 1, 5, 25, 10]
const abcd = points1.sort((a, b) => b - a)
console.log(abcd) // [100, 40, 25, 10, 5, 1]
-예시1-
const numbers = [1, 2, 3, 4]
const fruits = ['Apple', 'Banna', 'Cherry']
fruits.forEach(function(element, index, array) {
console.log(element, index, array)
})
<출력>
"Apple"
0
["Apple", "Banna", "Cherry"]
"Banna"
1
["Apple", "Banna", "Cherry"]
"Cherry"
2
["Apple", "Banna", "Cherry"]
-예시2- push 메서드가 원본을 직접 변경하기 때문에, 순회해서 빈 배열에 요소를 추가했을 때, 하나의 배열이 된다
const numbers1 = [1, 2, 3];
const pows = [];
numbers1.forEach(item => pows.push(item **2));
console.log(pows) // [1, 4, 9]
-예시3-
const fruits = ['Apple', 'Banna', 'Cherry']
fruits.forEach(function(f, i) {
console.log(f, i)
})
-출력
"Apple"
0
"Banna"
1
"Cherry"
2
-예시4-
const fruits = ['Apple', 'Banna', 'Cherry']
const a = fruits.forEach((fruit, i) => `[${fruit}-${i}]`)
console.log(a) // undefined
-예시1-
const fruits = ['Apple', 'Banna', 'Cherry']
const b = fruits.map(function(fruit, index) {
return `${fruit}-${index}`
})
console.log(b) // ["Apple-0", "Banna-1", "Cherry-2"]
console.log(fruits) // ["Apple", "Banna", "Cherry"]
-예시2-
const fruits = ['Apple', 'Banna', 'Cherry']
const c = fruits.map(function(fruit, index) {
return {
id: index,
name: fruit
}
})
console.log(c)
<출력>
(3) [{…}, {…}, {…}]
0: {id: 0 name: "Apple"}
1: {id: 1 name: "Banna"}
2: {id: 2 name: "Cherry"}
-예시3-
const numbers = [1, 2, 3, 4]
const d = numbers.map(number => number < 3)
console.log(d) // [true, true, false, false]
console.log(numbers) // [1, 2, 3, 4]
const numbers = [1, 2, 3, 4]
const e = numbers.filter(number => number < 3)
console.log(e) // [1, 2]
console.log(numbers) // [1, 2, 3, 4]
-예시1-
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found) // 12
-예시2-
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element < 5);
console.log(found) // undefined
-예시3-
const users = [
{id:1, name: 'Lee'},
{id:2, name: 'Kim'},
{id:2, name: 'Choi'},
{id:3, name: 'Park'}
]
const found1 = users.find(user => user.id === 2)
console.log(found1) // {id:2, name: 'Kim'}
-예시4-
const fruits = ['Apple', 'Banna', 'Cherry']
const ab = fruits.find(fruit => {
return /^B/.test(fruit)
})
console.log(ab) // "Banana"
-예시1-
const fruits = ['Apple', 'Banna', 'Cherry']
const cd = fruits.findIndex(fruit => {
return /^C/.test(fruit)
})
console.log(cd); // 2
-예시2-
const fruits = ['Apple', 'Banna', 'Cherry']
const cde = fruits.findIndex(fruit => {
return /^D/.test(fruit)
})
console.log(cde); // -1