0
3

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.

javascriptで多次元配列の重複を削除

Last updated at Posted at 2021-10-07

1・名前の重複を削除したい
2・名前が重複していた場合、moneyが2000のデータだけ欲しい

process.stdin.resume();
process.stdin.setEncoding('utf8');
var arr = [
    {id:1,name:"花子",money:5000},
    {id:2,name:"江藤",money:4000},
    {id:3,name:"山田",money:3000},
    {id:4,name:"山田",money:2000},
    {id:5,name:"山田",money:8888},
    {id:6,name:"山田",money:9999},
    {id:7,name:"松井",money:2000},
    {id:8,name:"松井",money:2093},
    {id:9,name:"松井",money:2090},
    {id:10,name:"大川",money:1000}
    ];
var check = [];
arr.forEach(function(list) {
    check[list.name] = (list.name in check) ? true : false;
});

var filtered = arr.filter(function (e) {
    return false === check[ e.name ] || true === check[ e.name ] && e.money === 2000;
} );
console.log( filtered );

#結果

[
  { id: 1, name: '花子', money: 5000 },
  { id: 2, name: '江藤', money: 4000 },
  { id: 4, name: '山田', money: 2000 },
  { id: 7, name: '松井', money: 2000 },
  { id: 10, name: '大川', money: 1000 }
]

0
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?