0

Could not find a working solution but I am creating a new object from some data inside a loop but the date contains double values so it should prevent this from adding to the new object. The name property should contain unique values only.

public listOfItems: {[key: number]: {name: string; inStock: boolean}} = {};
dataAndStuff.forEach((item, i) => {
    this.listOfItems[i] = {
        name: item.itemName,
        instock: false,
    };
});

// output
[
    {name: 'audi', inStock: true}
    {name: 'bmw', inStock: true}
    {name: 'audi', inStock: true}
    {name: 'kia', inStock: true}
]
user759235
  • 2,147
  • 3
  • 39
  • 77
  • listOfItems seems to be typed as an object, but in your output it looks like you want an array. – James Feb 02 '22 at 19:50
  • 2
    That is not a possible output of the code shown. The code shows an object with a `name` property and an `instock` property being set to `false`, but the output shows an object with a `name` property and an `inStock` property set to `true`. – Heretic Monkey Feb 02 '22 at 19:52
  • What is a duplicate? Is (audi, true) a duplicate of (audi, false)? Can that even happen? – Jonas Wilms Feb 02 '22 at 19:52
  • the name property cant contain double values – user759235 Feb 02 '22 at 19:55
  • Does this answer your question? [Create array of unique objects by property](https://stackoverflow.com/questions/18773778/create-array-of-unique-objects-by-property) – Heretic Monkey Feb 02 '22 at 20:01

0 Answers0