Array.forEach
1 2 3 4 5 |
const array = ['a', 'b', 'c']; array.forEach(element => console.log(element)); // expected output: "a" // expected output: "b" // expected output: "c" |
Array.map
1 2 3 4 5 6 7 8 9 |
arr.map(callback(currentValue[, index[, array]])[, thisArg]) const _array = [1, 4, 9, 16]; // pass a function to map const _map = _array.map(x => x * 2); console.log(_map); // expected output: Array [2, 8, 18, 32] |
Array.some callback이 어떤 요소라도 참인(true) 값을 반환하는 경우 true, 그 외엔 false
1 2 3 4 5 6 7 |
const array = [1, 2, 3, 4, 5]; // checks whether an element is even const even = (element) => element % 2 === 0; console.log(array.some(even)); // expected output: true |
Array.every callback이 모든 요소에 대해 참인(true) 값을 반환하는 경우 true, 그 외엔 false
1 2 3 4 5 6 |
const isBelowThreshold = (currentValue) => currentValue < 40; const array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every(isBelowThreshold)); // expected output: true |
Array.filter callback이 true를 반환하면 유지, false를 반환하면 삭제(버림)하여 새로운 배열을 반환
1 2 3 4 5 6 |
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter(word => word.length > 6); console.log(result); // expected output: Array ["exuberant", "destruction", "present"] |
Array.reduce
1 2 3 4 5 6 7 8 9 10 |
const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); // expected output: 10 // 5 + 1 + 2 + 3 + 4 console.log(array1.reduce(reducer, 5)); // expected output: 15 |