學習筆記:
方法
1. some()
2. every()
3. find()
4. findIndex()
5. splice()
some()應用
第1題
const Adult = people.some(person => new Date().getFullYear() - person.year >= 19)
console.log(Adult)
every()應用
第2題
const Adult = people.every(person => new Date().getFullYear() - person.year >= 19)
console.log(Adult)
find()應用
第3題
const comment = comments.find(coments => coments.id === 823423)
console.log(comment)
第5題
inventors.forEach(function(inventor){
inventor.years = inventor.passed-inventor.year
});
const yearsList = inventors.sort(function(a,b){
if ((a.passed - a.year) > (b.passed - b.year)){
return 1;
}else {
return -1;
}
})
console.table(yearsList);
//簡化 箭頭函式
inventors.forEach(inventor => inventor.years = inventor.passed-inventor.year);
const yearsList = inventors.sort((a,b) =>
(a.passed - a.year) > (b.passed - b.year) ? 1: -1 )
console.table(yearsList);
findIndex()應用 + splice()
第4題
const comment = comments.findIndex(coments => coments.id === 823423)
console.log(comment)
第5題
const comment = comments.findIndex(coments => coments.id === 823423)
const delComment = comments.splice(comment, 1)
console.log(comments)
第5題一樣是刪除不同用法,提供大家參考
//splice 直接在陣列中刪除,就不會恢復
const comment = comments.findIndex(coments => coments.id === 823423)
const delComment = comments.splice(comment, 1)
console.log(comments)
//slice 複製一個新陣列,拼接出刪除項目的前後,不影響原陣列
const newComment = [
...comments.slice(0, comment),
...comments.slice(comment+1)
]
console.log(newComment)