Array functions
Javascript 2022. 1. 23. 00:36https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array
const numbers = [1, 2, 3];
//모든 숫자가 2배 되가 되는 배열로 만들고 싶다
const numbers2 = numbers.map((num) => {
return num * 2;
});
console.log(numbers2);
console.log(numbers === numbers2); //false
기존 배열은 변하지 않고 새로운 배열이 만들어 졌다
주어진 콜백 함수를 가산기와 요소 각각에 대해 (왼쪽에서 오른쪽으로) 호출하여 하나의 값으로 줄인 결과를 반환합니다.
const array1 = [1, 2, 3, 4];
const reducer = (previousValue, currentValue) => previousValue + 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
'Javascript' 카테고리의 다른 글
원시데이터타입과 참조타입 (0) | 2022.01.23 |
---|---|
Destructuring 구조분해 할당 (0) | 2022.01.23 |
spead, rest operator (0) | 2022.01.22 |
infinite scrolling (0) | 2021.01.19 |
즉시실행함수(IIFE) : 익명의 함수 표현 (0) | 2020.09.28 |