Javascript:如何获取多个重复记录的唯一ID

时间:2023-02-07 07:37:01

say we have a object:

说我们有一个对象:

   var db = [
        {Id: "201" , Player: "Jon",price: "3.99", loc: "NJ" },
        {Id: "202", Player: "Sam",price: "4.22", loc: "PA" },
        {Id: "203" ,Player: "Sam",price: "4.22", loc: "NY" },
        {Id: "204", Player: "Bill",price: "3.22", loc: "TX" },
        {Id: "205" ,Player: "Dave",price: "3.99", loc: "WA" },
        {Id: "206" ,Player: "Dave",price: "3.99", loc: "WI" },
];

202,203,205,206 have similar values for player and price but I need just one id for similar values i.e, output should be 202,205.

202,203,205,206具有类似的玩家和价格值,但我只需要一个相似值的id,即输出应为202,205。

3 个解决方案

#1


1  

You can use reduce() first to return object with count of each Player|price combination. Then you can use another reduce, and check if count of Player|price number if > 1. If it is then add objects id to array.

您可以先使用reduce()返回具有每个Player | price组合数的对象。然后你可以使用另一个reduce,并检查Player的价格是否为> 1的数量。如果是,则将对象id添加到数组。

var db = [
   {Id: "201" ,Player: "Jon",price: "3.99", loc: "NJ" },
   {Id: "202", Player: "Sam",price: "4.22", loc: "PA" },
   {Id: "203" ,Player: "Sam",price: "4.22", loc: "NY" },
   {Id: "204", Player: "Bill",price: "3.22", loc: "TX" },
   {Id: "205" ,Player: "Dave",price: "3.99", loc: "WA" },
   {Id: "206" ,Player: "Dave",price: "3.99", loc: "WI" },
];

var obj = db.reduce(function(o, e) {
  o[e.Player + '|' + e.price] = (o[e.Player + '|' + e.price] || 0) + 1;
    return o;
}, {});

var result = db.reduce(function(r, e) {
  if(obj[e.Player + '|' + e.price] > 1) r.push(e.Id);
    return r;
}, []);

console.log(result)

#2


1  

var result = []; 

db.forEach(function(item, index){  
   db.forEach(function(item2, index2){ 
      if (index == index2) return;
      if (item.Player == item2.Player && item.price == item2.price) {   
         if(result.indexOf(item.Id) < 0){  
           result.push(item.Id);
         }
         if(result.indexOf(item2.Id) < 0){
           result.push(item2.Id);
         }
      }
   });
});

console.log(result); // [202,203,205,206]

I am not saying that it is the most correct decision ever, but it is working and understandable enough. You may check it yourself via this snippet with comments:

我并不是说这是有史以来最正确的决定,但它的工作和理解能力已经足够了。您可以通过以下评论自行检查:

https://codepen.io/anon/pen/vKWZKx

#3


0  

You can get the ID with simple loop.

您可以通过简单的循环获取ID。

var db = [
        {Id: "201" , Player: "Jon",price: "3.99", loc: "NJ" },
        {Id: "202", Player: "Sam",price: "4.22", loc: "PA" },
        {Id: "203" ,Player: "Sam",price: "4.22", loc: "NY" },
        {Id: "204", Player: "Bill",price: "3.22", loc: "TX" },
        {Id: "205" ,Player: "Dave",price: "3.99", loc: "WA" },
        {Id: "206" ,Player: "Dave",price: "3.99", loc: "WI" },
];

for(var i = 0; i < db.length; i++){
  var obj = db[i];
  console.log('Id: ', obj.Id);
}

#1


1  

You can use reduce() first to return object with count of each Player|price combination. Then you can use another reduce, and check if count of Player|price number if > 1. If it is then add objects id to array.

您可以先使用reduce()返回具有每个Player | price组合数的对象。然后你可以使用另一个reduce,并检查Player的价格是否为> 1的数量。如果是,则将对象id添加到数组。

var db = [
   {Id: "201" ,Player: "Jon",price: "3.99", loc: "NJ" },
   {Id: "202", Player: "Sam",price: "4.22", loc: "PA" },
   {Id: "203" ,Player: "Sam",price: "4.22", loc: "NY" },
   {Id: "204", Player: "Bill",price: "3.22", loc: "TX" },
   {Id: "205" ,Player: "Dave",price: "3.99", loc: "WA" },
   {Id: "206" ,Player: "Dave",price: "3.99", loc: "WI" },
];

var obj = db.reduce(function(o, e) {
  o[e.Player + '|' + e.price] = (o[e.Player + '|' + e.price] || 0) + 1;
    return o;
}, {});

var result = db.reduce(function(r, e) {
  if(obj[e.Player + '|' + e.price] > 1) r.push(e.Id);
    return r;
}, []);

console.log(result)

#2


1  

var result = []; 

db.forEach(function(item, index){  
   db.forEach(function(item2, index2){ 
      if (index == index2) return;
      if (item.Player == item2.Player && item.price == item2.price) {   
         if(result.indexOf(item.Id) < 0){  
           result.push(item.Id);
         }
         if(result.indexOf(item2.Id) < 0){
           result.push(item2.Id);
         }
      }
   });
});

console.log(result); // [202,203,205,206]

I am not saying that it is the most correct decision ever, but it is working and understandable enough. You may check it yourself via this snippet with comments:

我并不是说这是有史以来最正确的决定,但它的工作和理解能力已经足够了。您可以通过以下评论自行检查:

https://codepen.io/anon/pen/vKWZKx

#3


0  

You can get the ID with simple loop.

您可以通过简单的循环获取ID。

var db = [
        {Id: "201" , Player: "Jon",price: "3.99", loc: "NJ" },
        {Id: "202", Player: "Sam",price: "4.22", loc: "PA" },
        {Id: "203" ,Player: "Sam",price: "4.22", loc: "NY" },
        {Id: "204", Player: "Bill",price: "3.22", loc: "TX" },
        {Id: "205" ,Player: "Dave",price: "3.99", loc: "WA" },
        {Id: "206" ,Player: "Dave",price: "3.99", loc: "WI" },
];

for(var i = 0; i < db.length; i++){
  var obj = db[i];
  console.log('Id: ', obj.Id);
}