check unique object in array javascript site:stackoverflow.com 1

check unique object in array javascript site:stackoverflow.com

var data = [
{name:"Joe", date:'2018-07-01', amt:250 },
{name:"Mars", date:'2018-07-01', amt:250 },
{name:"Joe", date:'2018-07-02', amt:250 },
{name:"Saturn", date:'2018-07-01', amt:250 },
{name:"Joe", date:'2018-07-02', amt:250 },
{name:"Jupiter", date:'2018-07-01', amt:250 },
]
var resArr = [];
data.filter(function(item){
  var i = resArr.findIndex(x => (x.name == item.name && x.date == item.date && x.amt == item.amt));
  if(i <= -1){
        resArr.push(item);
  }
  return null;
});
console.log(resArr)

Here is what the above code is Doing:
1. We have an array of objects called data.
2. We create an empty array called resArr.
3. We use the filter function to filter out the duplicate objects.
4. We use the findIndex function to find the index of the duplicate object.
5. If the index is not found, we push the object into the resArr array.
6. We return the resArr array.

Similar Posts