I have an array of objects with the same keys, I want to create a new array of objects that creates a new object that combines the uniques ones together into one
const arrayOfObjects = [
{ 'Serial Number': '90946117350061093222581604839920' },
{ 'Serial Number': '77362117350061093222581604891733' },
{ 'Serial Number': '77362117350061093222581604891734' },
{ 'Part Number': 'TR40-60C779-AA' },
{ 'Part Number': 'ST41-60C780-AA' },
{ 'Part Number': 'QT41-60C780-AA' },
{ 'Cell Count': '28' },
{ 'Cell Count': '27' },
{ 'Cell Count': '29' },
{ 'Length': '10' },
{ 'Length': '11' },
{ 'Length': '11' },
]
So it needs to look like the below.
const desiredArrayOfObjects = [
{
'Serial Number': '90946117350061093222581604839920',
'Part Number': 'TR40-60C779-AA',
'Cell Count': '27',
'Length': '10',
},
{
'Serial Number': '77362117350061093222581604891733',
'Part Number': 'ST41-60C780-AA',
'Cell Count': '28',
'Length': '11',
},
{
'Serial Number': '77362117350061093222581604891734',
'Part Number': 'QT41-60C780-AA',
'Cell Count': '29',
'Length': '12',
},
]
I have tried it with the code below, but i'm clearly not getting it right, help is welcome and appreciated.
let newArr = [];
arrayOfObjects.map((x,i) => {
Object.entries(x).map(([key, value]) => {
if(!newArr.length){
if(Object.keys(newArr[i]).toString() === key){
newArr.push({[key]: value})
}
}
})
})