0

I have the an array, listPeople and to eliminate duplicates I use the following code in ES6:

var listPeople = [{
    "nomCustomer": "Fran",
    "attrName": "aaa"
  },
  {
    "nomCustomer": "John",
    "attrName": "ccc"
  },
  {
    "nomCustomer": "Paul",
    "attrName": "ddd"
  },
  {
    "nomCustomer": "Paul",
    "attrName": "ddd"
  }
];

var list = listPeople.reduce((unique, o) => {
  if (!unique.some(obj => obj.nomCustomer === o.nomCustomer && obj.attrName === o.attrName)) {
    unique.push(o);
  }
  return unique;
}, []);

console.log(list)

What would it be like for ES5?

9
  • 5
    the same without without the arrow function ...? Commented Jan 20, 2020 at 14:18
  • 1
    ES5 includes .reduce() Commented Jan 20, 2020 at 14:18
  • 2
    Have you checked babel babeljs.io/en/repl ? I guess instead of asking you could have convert it online. Commented Jan 20, 2020 at 14:22
  • What's the harm in leaving this open for answers? Commented Jan 20, 2020 at 14:38
  • @Ben what's the benefit? We already have a canonical for getting uniques from array. Moreover, the code here works and translating it to ES5 is trivial. It also has duplicates. Any reason another copy of existing answers is needed? Commented Jan 20, 2020 at 14:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.