[JS30] Day07: Array Cardio Day 2
這邊介紹幾個 Array 的操作
-
some
回傳是否至少一個元素滿足給定條件 題目是 is at least one person 19 or order?
-
every
回傳是否每個元素滿足給定條件 題目是 is everyone 19 or older?
-
find
回傳第一個滿足給定條件的元素 Find is like filter, but instead returns just the one you are looking for 題目是 find the comment with the ID of 823423
-
findIndex
回傳滿足給定條件的元素的索引
// Find the comment with this ID
// delete the comment with the ID of 823423
const find823423Index = comments.findIndex((comment) => comment.id === 823423);
// 第一種刪 除方法
comments.splice(find823423Index, 1);
// 第二種刪除方法
const newComments = [
...comments.slice(0, find823423Index),
...comments.slice(find823423Index + 1),
];