Array.prototype.some()
Array.prototype.every()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.splice(start,deleteCount,item1,item2, ...)
Array.prototype.slice()
를 사용할 수 있다.
## Array Cardio Day 2
const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 },
];
const comments = [
{ text: 'Love this!', id: 523423 },
{ text: 'Super good', id: 823423 },
{ text: 'You are the best', id: 2039842 },
{ text: 'Ramen is my fav food ever', id: 123523 },
{ text: 'Nice Nice Nice!', id: 542328 },
];
// 1. Some and Every Checks
// Array.prototype.some() // is at least one person 19 or older?
// 하나라도 true면 true
const haveAdult = people.some((person) => {
const year = new Date().getFullYear();
return year - person.year >= 19;
});
console.log(haveAdult); // true
// Array.prototype.every() // is everyone 19 or older?
// 전부다 true인지 확인
const isAllAdult = people.every((person) => {
return new Date().getFullYear() - person.year >= 19;
});
console.log(isAllAdult); // false
// 2. Array.prototype.find()
// Find is like filter, but instead returns just the one you are looking for
// filter와 비슷하지만, 맨 처음 찾는 요소만 리턴해준다.
// find the comment with the ID of 823423
const comment = comments.find((comment) => comment.id === 823423);
console.log(comment); // {text: 'Super good', id: 823423}
// 3. Array.prototype.findIndex()
// Find the comment with this ID
// 찾는 요소의 index를 리턴
// delete the comment with the ID of 823423
const index = comments.findIndex((comment) => comment.id === 823423);
// Array.prototype.splice(start,deleteCount,item1,item2, ...) 시작인덱스, 삭제할 갯수, 추가할 item들
// 배열요소 삭제
const deleted = comments.splice(index, 1); // index부터, 1개를 삭제
// 배열요소 추가
comments.splice(index, 0, ...deleted); // index부터 0개삭제, 요소추가
// Array.prototype.slice() and ES6 spread operator
// 배열요소 삭제 2
const newComments = [
...comments.slice(0, index),
...comments.slice(index + 1),
];
console.log(newComments);
/*
[
0: {text: 'Love this!', id: 523423}
1: {text: 'You are the best', id: 2039842}
2: {text: 'Ramen is my fav food ever', id: 123523}
3: {text: 'Nice Nice Nice!', id: 542328}
]
*/