본문 바로가기

Array Method 정리1 ( concat(), fill(), filter() )

by meno1011 2022. 8. 29.
728x90

1) concat() : 인자로 주어진 배열이나 값들을 기존 배열에 합쳐서 새 배열로 반환한다.

// Array.concat(value1, value2 ... )
const A = [1, 2, 3];
const B = [4, 5, 6];
const C = A.concat(B);
console.log(C); // [ 1, 2, 3, 4, 5, 6 ]

2) fill() : 배열의 시작 인덱스부터 끝 인덱스의 이전까지 정적인 값 하나로 채운다.

// Array.fill(value, start, end)
const arr = [1, 2, 3, 4];
console.log(arr.fill(0, 2, 4)); // [ 1, 2, 0, 0 ]
console.log(arr.fill(5, 1)); // [ 1, 5, 5, 5 ]
console.log(arr.fill(6)); // [ 6, 6, 6, 6 ]

3) filter() : 주어진 함수의 테스트를 통과하는 모든 요소들을 모아 새 배열로 반환한다.

// Array.filter(callback(element, index))
const arr = [1, 2, 3, 4, 5];
console.log(arr.filter((element, index) => element > 3)); // [ 4, 5 ]
console.log(arr.filter((element, index) => index > 3)); // [ 5 ]

 

'' 카테고리의 다른 글

Array Method 정리3 ( from(), includes(), indexOf(), join(), map() )  (0) 2022.09.01
Array Method 정리2 ( find(), findIndex(), flat(), forEach() )  (0) 2022.08.30
yield  (0) 2022.08.24
function*  (0) 2022.08.23
클로저  (0) 2022.07.01