OKADA LABO

Javascriptでobjectの配列をfilterする記述例

Javascriptで配列Arrayの中身がobject{xxx:1, yyy:"hoge"} のものを.filterする記述例。

const books = [
  { id: 1, title: "title1" },
  { id: 2, title: "title2" },
  { id: 3, title: "title3" },
  { id: 4, title: "title4" }
];

const deleteTargetId = 4;

const deletedArray = books.filter(
  (book) => {
    return book.id !== deleteTargetId;
  }
);

console.log(deletedArray);

アロー関数をシンプルに。()は省略

const deletedArray = books.filter(
  book => {
    return book.id !== deleteTargetId;
  }
);

1行なので {}とreturnを省略

const deletedArray = books.filter(book => 
  book.id !== deleteTargetId
);
const deletedArray = books.filter(book => book.id !== deleteTargetId);