1.filter
특정 조건에 만족하는 값을 찾아서 그 원소들을 가지고 새로운 배열로 출력해줍니다.
const items=[{
id:1,
text: 'hello',
done: true
}, {
id:2,
text: 'bye',
done: true
},{
id:3,
text: 'good',
done: true
},{
id:3,
text: 'nice',
done: false
}];
const tasksNotDone = items.filter(item=>!item.done);
console.log(tasksNotDone);
//출력값
[ { id: 3, text: 'nice', done: false } ]
item=>!item.done 은 item=>item.done===false와 같은 말입니다.
2.splice
지우고싶은 데이터를 범위를 잡아서 지울 수 있습니다.
const numbers = [1,2,3,4,5,6,7,8];
const index = numbers.indexOf(3);
const spliced = numbers.splice(index,2);
console.log(spliced);
console.log(numbers);
//출력값
[ 3, 4 ]
[ 1, 2, 5, 6, 7, 8 ]
우선 지우고싶은데이터를 지정합니다. index라고 지정했으면 splice(index,2)의 뜻은 index라고 지정한 데이터부터 2개의 데이터를 없앨것이라는 뜻입니다.
3. slice
지우고 싶은 데이터범위를 정할 수 있습니다.
const numbers = [1,2,3,4,5,6,7,8];
const sliced = numbers.slice(3,5);
console.log(sliced);
console.log(numbers);
//출력값
[ 4, 5 ]
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
splice랑 비슷하지만, 기존의 배열을 건들지않는다는 특징이 있습니다. slice(3,5) 뜻은 3번째 인덱스부터 5번째 인덱스 전까지만 자르겠다는 뜻입니다.
'Javascript > 배열' 카테고리의 다른 글
Javascript 배열 내장함수(reduce) 숫자와 문자 다루기 (0) | 2021.02.01 |
---|---|
Javascript 배열 내장함수(shift, pop, unshift, push, concat, join) (0) | 2021.01.31 |
Javascript 배열 내장함수(forEach, map, indexOf, findIndex, find) (0) | 2021.01.31 |
Javascript 배열 반복문 (for...of, for...in) (0) | 2021.01.31 |
Javascript 배열의 특징(push, length) (0) | 2021.01.31 |