LaVOZs

The World’s Largest Online Community for Developers

'; javascript - remove objects from array by comparing value1 of elements that share value2 - LavOzs.Com

I have many objects in an array. I'd like to take any objects that have the same 'id' and compare the 'num' value with other 'num' values of that same 'id'. I'm looking to reduce the entire array of objects to an array of objects with only the highest number per id. These can be duplicated but any objects that have less than the highest number should be removed. All other key/value pairs should come along for the ride as well in whatever final array of objects is created.

{
  random : ajabl,
  num : 5, // should stay, highest number is 5
  id :10
}
{
  random: jlsl,
  num : 4, // should go, highest number is 5
  id : 10 
}
{
  random: jlssssl,
  num : 5, // should stay, highest number is 5
  id : 10
}
{ 
  random: abcd,
  num : 1, // should go, highest number is 5
  id : 10
}
{
  random : xy45,
  num : 2, // Should stay, highest number is 2
  id : 11
}

I think you can achieve it using ES6 by this way:

let arr = [{
  num : 5, // should stay, highest number is 5
  id :10
},
{
  num : 4, // should go, highest number is 5
  id : 10 
},
{
  num : 5, // should stay, highest number is 5
  id : 10
},
{ 
  num : 1, // should go, highest number is 5
  id : 10
},
{
  num : 2, // Should stay, highest number is 2
  id : 11
}];


let ids = Array.from(new Set(arr.map(i => i.id)));

let result = [];
Array.from(new Set(arr.map(i => i.id))).forEach(a => {
  let tmp = arr.filter(i => i.id === a).sort((a,b) => b.num - a.num);
  tmp.filter(i => i.num === tmp[0].num).map(j => result.push(j));
});

console.log(result);

I'm sure there are far better ways to do this, but hey, it's a start :)

var list = [
{
  num : 5, // should stay, highest number is 5
  id :10
},
{
  num : 4, // should go, highest number is 5
  id : 10 
},
{
  num : 5, // should stay, highest number is 5
  id : 10
},
{ 
  num : 1, // should go, highest number is 5
  id : 10
},
{
  num : 2, // Should stay, highest number is 2
  id : 11
}
]
let result = []
for(obj of list) {
    let resultItem = result.find(item => item.id==obj.id)
    if(resultItem==null) {
        //first occurence of this id, keep it
        result.push(obj)
    } else {
        //keep it only if it's greater or equal than the existing num
        if(obj.num >= resultItem.num) {
            result.push(obj)
            //remove the previous ones
            result = result.filter(item => item.id!=obj.id || item.num==obj.num)
        }
    }
}
console.log(result)

var a = [
  {
    num: 5, // should stay, highest number is 5
    id: 10
  },
  {
    num: 4, // should go, highest number is 5
    id: 10
  },
  {
    num: 5, // should stay, highest number is 5
    id: 10
  },
  {
    num: 1, // should go, highest number is 5
    id: 10
  },
  {
    num: 2, // Should stay, highest number is 2
    id: 11
  }
];

var d = a.reduce((c, b) => {
  if (!c[b["id"]]) {
    c[b["id"]] = b["num"];
  } else {
    if (c[b["id"]] < b["num"]) {
      c[b["id"]] = b["num"];
    }
  }
  return c;
}, {});

var e = Object.keys(d).map(function(key) {
  return { id: key, num: d[key] };
});

console.log(e);