4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

二次元配列から値の重複した要素を削除する

Last updated at Posted at 2015-06-19

オブジェクトの配列から重複した値を持つ要素を削除する方法。
毎回やり方を忘れるのでメモ。

removeOverlapValue.js
var overlapped = [
  { key: "value" },
  { key: "value2" },
  { key: "value" } // 削除対象
];
var values = [];

var no_overlapped = overlapped.filter(e => {
  if (values.indexOf(e["key"]) === -1) {

    // values に値が存在しない要素のみをフィルタリング
    values.push(e["key"]);
    return e;
  }
});
/*
 * { key: "value" },
 * { key: "value2" }
 */
4
6
3

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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?