Im trying to build a new array of objects based on specs array of objects.
There are some similar question already but didn't help me.
const specs =
[
{ label: 'Brand', value: 'Nike' },
{ label: 'Age range', value: '8 to 13' },
{ label: 'Age range', value: '14+' }
]
I want to skip the "Brand", which works with my code, and I want that if there are multiple objects with the same label, they need
to be merged in a unique object which contains that label and ALL of the values together, so the output will have brand removed and the other values merged:
[
{ label: 'Age range', value: '8 to 13 - 14+' }
]
Here's my code:
var newSpecs = []
for (let x = 0; x < specs.length; x++) {
if (specs[x].label === 'Brand') continue
for (let j = x + 1; j < specs.length; j++) {
if (specs[x].label === specs[j].label) {
specs[x].value += ' - ' + specs[j].value
}
}
newSpecs.push({ label: specs[x].label, value: specs[x].value })
}
return newSpecs
But this simply does not work, it created multiple merged age range.
I tried another time by using delete and other things but got me "cannot read label of undefine", so I'll keep this way,
but I cannot figure out how to do it
[ { label: 'Age range', value: '8 to 13 - 14+' }, { label: 'Age range', value: '14+' } ]@MRMarkII