[
{ "id":5,
"name": "banana"
},
{ "id":5,
"name": "banana"
},
{ "id":4,
"name": "apple"
},
{ "id":1,
"name": "strawberry"
},
{ "id":1,
"name": "strawberry"
}
]
const arrayMap = new Map(Array.map(i => [i.id, {...i, amount: 0 }]));
Only have one method I’ve considered, such as mapping the array and assigning an amount per object using a loop but I’m not sure if this is a good way to do it, and the string part is still something I haven’t been able to figure out.
Order would be by highest ID number. The array could be seen as a shopping cart, you can have multiple bananas, and they’d just be listed under the same part of the string.
If you have 2 bananas with the ID 5, 1 apple with the ID 4, and 2 strawberries with the ID 1 it would display as such:
However, in my example the "banana" isn’t what makes it unique, it would be the ID. There would be no other bananas with different IDs, they would all be added with the same ID as duplicate objects.
"2x Banana, 1x Apple, 2x Strawberry"
2
Answers
You can do it by looping through the array. For example,
name
with count values.