1. findLast / findLastIndex
find() 는 앞에서부터 조건을 만족하는 첫 요소를 찾고,
findLast() 는 뒤에서부터 찾는다.
const numbers = [1, 2, 3, 4, 5, 6];
console.log(numbers.find(n => n % 2 === 0)); // 2
console.log(numbers.findLast(n => n % 2 === 0)); // 6
console.log(numbers.findLastIndex(n => n % 2 === 0)); // 5
2. toSorted / toReversed / toSpliced / with
원본 배열은 변경하지 않고, 새로운 배열을 반환한다.
React 같이 불변성 유지가 중요한 프레임워크에서 유용히 쓰임.
const arr = [3, 1, 4];
// toSorted
console.log(arr.toSorted()); // [1, 3, 4]
// toReversed
console.log(arr.toReversed()); // [4, 1, 3]
// toSpliced(1, 1) → 1번 인덱스에서 1개 삭제
console.log(arr.toSpliced(1, 1)); // [3, 4]
// with(1, 8) → 1번 인덱스의 값을 8로 변경
console.log(arr.with(1, 8)); // [3, 8, 4]
console.log(arr); // [3, 1, 4] ← 원본 그대로!
arr.toSorted()
: 정렬된 새 배열 반환
arr.toReversed()
: 역순인 새 배열 반환
arr.toSpliced(start, deleteCount)
: splice() 처럼 작동하지만 원본 유지
: 삭제된 요소를 제외한 새 배열 반환
arr.with(index, value)
: 특정 인덱스 값을 교체한 새 배열 반환
1. findLast / findLastIndex
find() 는 앞에서부터 조건을 만족하는 첫 요소를 찾고,
findLast() 는 뒤에서부터 찾는다.
const numbers = [1, 2, 3, 4, 5, 6];
console.log(numbers.find(n => n % 2 === 0)); // 2
console.log(numbers.findLast(n => n % 2 === 0)); // 6
console.log(numbers.findLastIndex(n => n % 2 === 0)); // 5
2. toSorted / toReversed / toSpliced / with
원본 배열은 변경하지 않고, 새로운 배열을 반환한다.
React 같이 불변성 유지가 중요한 프레임워크에서 유용히 쓰임.
const arr = [3, 1, 4];
// toSorted
console.log(arr.toSorted()); // [1, 3, 4]
// toReversed
console.log(arr.toReversed()); // [4, 1, 3]
// toSpliced(1, 1) → 1번 인덱스에서 1개 삭제
console.log(arr.toSpliced(1, 1)); // [3, 4]
// with(1, 8) → 1번 인덱스의 값을 8로 변경
console.log(arr.with(1, 8)); // [3, 8, 4]
console.log(arr); // [3, 1, 4] ← 원본 그대로!
arr.toSorted()
: 정렬된 새 배열 반환
arr.toReversed()
: 역순인 새 배열 반환
arr.toSpliced(start, deleteCount)
: splice() 처럼 작동하지만 원본 유지
: 삭제된 요소를 제외한 새 배열 반환
arr.with(index, value)
: 특정 인덱스 값을 교체한 새 배열 반환