1
1

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 3 years have passed since last update.

連想配列の重複チェック 【Lodash, find】

Last updated at Posted at 2020-07-09

概要

LodashのfindとArray.filterを利用して、連想配列の重複チェック&重複している物の配列と重複していない物の配列を作成します。

実装


const targetA = [
  {name: "taro"},
  {name: "ichiro"},
  {name: "tetsuto"},
  {name: "lisa"}
];

const targetB = [
  {name: "yuki"},
  {name: "ichiro"},
  {name: "takeya"},
  {name: "lisa"}
];

// 重複してるユーザーを取得
const duplicatedList = targetA.filter(user => {
  return _.find(targetB, user);
});

// 重複していないユーザーを取得
const originalList = targetA.filter(user => {
  return  !_.find(targetB, user);
});

結果

スクリーンショット 2020-07-09 11.52.15.png
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?