Array Method 정리6 ( slice(), sort() ) 끝
1) slice() : 어떤 배열의 begin부터 end까지에 대한 얕은 복사본을 반환한다. (단, 원본 배열은 바뀌지 않는다.) // Array.slice(begin, end) const arr = [1, 2, 3, 4, 5]; console.log(arr.slice(2)); // [ 3, 4, 5 ] console.log(arr); // [ 1, 2, 3, 4, 5 ] 원본 배열은 바뀌지 않는다. console.log(arr.slice(2, 4)); // [ 3, 4 ] console.log(arr.slice(1, 5)); // [ 2, 3, 4, 5 ] console.log(arr.slice(-2)); // [ 4, 5 ] console.log(arr.slice(2, -1)); // [ 3, 4 ]..
2022. 9. 6.
Array Method 정리5 ( pop(), push(), reverse(), shift() )
1) pop() : 배열의 마지막 요소를 제거하고 제거된 요소를 반환한다. // Array.pop() const arr = [1, 2, 3]; console.log(arr.pop()); // 3 console.log(arr); // [1, 2] arr.pop(); console.log(arr); // [1] 2) push() : 배열의 끝에 하나 이상의 요소를 추가하고, 배열의 새로운 길이를 반환한다. // Array.push(element1, element2, ...) const arr = [1, 2, 3]; const len = arr.push(4); // 길이 console.log(len); // 4 console.log(arr); // [1, 2, 3, 4] 3) reverse() : 배열의 순서..
2022. 9. 5.
Array Method 정리4 ( reduce() )
reduce() : 이 메서드는 배열의 각 요소에 대해 주어진 리듀서(reducer) 함수를 실행하고, 하나의 결과값을 반환한다. // Array.reduce(callback, initialValue); // Array.reduce(function(accumulator, currentValue, currentIndex, array){ // return accumulator + currentValue // },initialValue) const initialValue = 10; const arr = [0, 1, 2, 3, 4]; const emptyArr = []; 배열이 비어있지 않았을 때 initialValue를 제공하지 않았을 경우 accumulator는 배열의 첫번째 값을 가져오며 currentVa..
2022. 9. 5.