mapThe map() method creates a new array with the results of calling a provided function on every element in this array.
각각 배열 원소들에 대해서 한번씩 순서대로 불러 그 함수의 반환값으로 새로운 배열을 만든다
예시
/1/
var kvArray = [{key:1, value:10},
{key:2, value:20},
{key:3, value: 30}];
var reformattedArray = kvArray.map(function(obj){
var rObj = {};
rObj[obj.key] = obj.value;
return rObj;
});
// reformattedArray는 [{1:10}, {2:20}, {3:30}]
// kvArray는 그대로
// [{key:1, value:10},
// {key:2, value:20},
// {key:3, value: 30}]
/2/
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots는 [1, 2, 3]
// numbers는 그대로 [1, 4, 9]
filterThe filter() method creates a new array with all elements that pass the test implemented by the provided function.
주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환
예시
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"]
findThe find() method returns a value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.
주어진 판별 함수를 만족하는 첫 번째 요소의 값을 반환 / 그런 요소가 없다면 undefined 반환
예시
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// Expected output: 12
everyThe every() method tests whether all elements in the array pass the test implemented by the provided function.
배열 안의 모든 요소가 주어진 판별 함수를 통과하는지 테스트 함 / Boolean 값 반환
예시
/1/
const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// Expected output: true
/2/
const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 50, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// Expected output: false
someThe some() method tests whether some element in the array passes the test implemented by the provided function.
배열 안의 어떤 요소라도 주어진 반별 함수를 적어도 하나라도 통과하는지 테스트함 / Boolean 반환
예시
/1/
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
/2/
const array = [1, 1, 1, 1, 1];
// Checks whether an element is even
const even = (element) => element % 2 === 0;
console.log(array.some(even));
// Expected output: false
reduceThe reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
배열의 각 요소에 대해 주어진 reducer 함수를 실행하고, 하나의 결과값 반환
예시
const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue
);
console.log(sumWithInitial);
// Expected output: 10