I have an array of objects which have the same structure except that some values have arrays as the type to signify that it has multiple values. I need to create a new object in that array based on how many items in the array.
const arrayOfObjects = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": [
"90946117350061093222581604839920",
"77362117350061093222581604891733",
"77362117350061093222581604891734",
],
"Part Number": [
"TR40-60C779-AA",
"ST41-60C780-AA",
"ZT41-60C780-AA",
],
"Cell Count": [
"28",
"27",
"26"
],
"Length": [
"10",
"11",
"12"
]
}
]
Below is how i want to transform it
const transformedArray = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": "90946117350061093222581604839920",
"Part Number": "TR40-60C779-AA",
"Cell Count": "28",
"Length": "10"
},
{
"Serial Number": "77362117350061093222581604891733",
"Part Number": "ST41-60C780-AA",
"Cell Count": "27",
"Length": "11"
},
{
"Serial Number": "77362117350061093222581604891734",
"Part Number": "ZT41-60C780-AA",
"Cell Count": "26",
"Length": "12"
},
]
I have tried to solve it with the below code attempt, please help complete:
let newItemsArray = [];
arrayOfObjects.map((item,index) => {
return Object.entries(item).map(([key,value]) => {
if(Array.isArray(value)){
value.forEach(x => {
newItemsArray.push({[key]:x})
})
}
})
})