Skip to main content

[JS30] Day07: Array Cardio Day 2

這邊介紹幾個 Array 的操作

  1. some

    回傳是否至少一個元素滿足給定條件 題目是 is at least one person 19 or order?

  2. every

    回傳是否每個元素滿足給定條件 題目是 is everyone 19 or older?

  3. find

    回傳第一個滿足給定條件的元素 Find is like filter, but instead returns just the one you are looking for 題目是 find the comment with the ID of 823423

  4. 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),
];