1

Is there a way to remove dupilicates making sure that the nth index is always left. So if I have

[{name: "john", age:20},{name:"john", age:30}]

I would need the result to always be

[{name:"john",age:30}]

currently, I'm using this method of removing dups

  PICKER.saveSelection = PICKER.saveSelection.filter((select, index, self) => {
 return self.map(function(mapItem){ return mapItem["category"]; }).indexOf(select["category"]) === index;});

1 Answer 1

4

You can use reduce and group the array into an object using the name as the key. Use Object.values to convert back the object into an array.

var arr = [
      {name: "john", age:20},
      {name:"john", age:30},
      {name:"adam", age:30},
      {name:"eddie", age:27}
];

var result = Object.values(arr.reduce((c, v) => Object.assign(c, {[v.name]: v}), {}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.