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?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Eladerezador
  • 1,277
  • 7
  • 25
  • 48
  • 5
    the same without without the arrow function ...? – Nina Scholz Jan 20 '20 at 14:18
  • 1
    ES5 includes `.reduce()` – Pointy Jan 20 '20 at 14:18
  • 2
    Have you checked babel https://babeljs.io/en/repl ? I guess instead of asking you could have convert it online. – Sonu Bamniya Jan 20 '20 at 14:22
  • What's the harm in leaving this open for answers? – Ben Aston Jan 20 '20 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](https://stackoverflow.com/questions/37668185/converting-ecmascript-6s-arrow-function-to-a-regular-function). Any reason another copy of existing answers is needed? – VLAZ Jan 20 '20 at 14:55
  • We can both link to the canonical answer and supply a specific answer for this particular flavour of the question. I don't see a conflict there. The benefit is that it delivers a more positive overall outcome. The questioner gets what they want and contributors get the chance to help. – Ben Aston Jan 20 '20 at 15:04
  • @Ben I personally see no value in just posting re-posting the same content in different places. That's why we have dupes. Feel free to run it by [meta] to verify, though. – VLAZ Jan 20 '20 at 15:23
  • It's not the same content. It is related content. Maybe tightly so. But different content. – Ben Aston Jan 20 '20 at 15:34
  • `_.uniqBy(listPeople, e => e.nomCustomer && e.attrName)` https://lodash.com/docs/4.17.15#uniqBy Do not underestimate the power of lodash. With tools like webpack and the ability to perform tree shaking, bringing lodash into your project will save you so much time at little to no cost relative to the time savings – dillon Jan 21 '20 at 03:15

0 Answers0