LoginSignup
0
0

More than 3 years have passed since last update.

JSで配列の検索と削除のメモ

Posted at

よく使う機会あったので備忘メモ
配列のobjectでID一致したらそこを削除するメモ

一致データを削除サンプル

datas = [{id:1,content:"AAA"},{id:2,content:"BBB"},{id:3,content:"CCC"}];

deleteData (id) {
  const result = this.datas.findIndex(
    (item) => item.id === id
  );
  if (-1 < result) {
    this.datas.splice(result, 1);
  }
}

参考サイト

ES6でよく使う、配列の中身を操作・検索する関数
https://qiita.com/pentla/items/a87383903a30d7baa282#findindex
JavaScript - 配列の要素を削除する
https://murashun.jp/blog/20191110-18.html#chapter-3

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0