I have an array of objects that i need to flatten/simplify/combine based on certain conditions. Here is the shape of my current array of objects below:
const arrayOfObjects = [
{
"Battery": "Battery"
},
{
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number",
]
},
{
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 100 QC",
"Extended Range",
"4P94-Q001",
]
},
{
"Modules": "Modules"
},
{
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count",
]
},
{
"moduleDetailsVal": [
"83675327350061093222581609899001",
"LJ98-10C779-A01",
"32",
]
},
{
"assetSeparator": "assetSeparator"
},
{
"Battery": "Battery"
},
{
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number"
]
},
{
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 101 QC",
"Extended Range",
"4P94-Q002"
]
},
{
"Modules": "Modules"
},
{
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count"
]
},
{
"moduleDetailsVal": [
"83675327350061093222581609899002",
"LJ98-10C779-A02",
"28"
]
},
{
"moduleDetailsVal": [
"83675327350061093222581609899003",
"LJ98-10C779-A03",
"27"
]
},
{
"assetSeparator": "assetSeparator"
},
{
"Battery": "Battery"
},
{
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number",
]
},
{
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 102 QC",
"Extended Range",
"4P94-Q003",
]
},
{
"Modules": "Modules"
},
{
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count",
]
},
{
"moduleDetailsVal": [
"83675327350061093222581609899004",
"LJ98-10C779-A01",
"32",
]
}
]
I basically want this arrayOfObjects to be shaped into this structure:
const shapeIWantArrayOfObjects = [
{
"Battery": "Battery",
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number",
],
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 100 QC",
"Extended Range",
"4P94-Q001",
],
"Modules": "Modules",
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count",
],
"moduleDetailsVal": [
"83675327350061093222581609899001",
"LJ98-10C779-A01",
"32",
]
},
{
"Battery": "Battery",
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number"
],
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 101 QC",
"Extended Range",
"4P94-Q002"
],
"Modules": "Modules",
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count"
],
"moduleDetailsVal": [
"83675327350061093222581609899002",
"LJ98-10C779-A02",
"28"
],
"moduleDetailsVal": [
"83675327350061093222581609899003",
"LJ98-10C779-A03",
"27"
]
},
{
"Battery": "Battery",
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number",
],
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 102 QC",
"Extended Range",
"4P94-Q003",
],
"Modules": "Modules",
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count",
],
"moduleDetailsVal": [
"83675327350061093222581609899004",
"LJ98-10C779-A01",
"32",
]
}
]
As you can see i’m basically wanting to combine the modules and batteries details into one object, then as you can see i want to create another object within that array once i hit the {"assetSeparator": "assetSeparator"}. That’s like my conditional that tells me that asset has been combined, now time to combine the next one, almost think of it as string.split("assetSeparator")
Can someone please tell me how i could achieve this, i’ve tried Object.assign({}, ...arrayOfObjects) but that didn’t quite achieve what i want, and i can’t detect the {"assetSeparator": "assetSeparator"} using the spread operator.
I’ve also tried doing a reduce arrayOfObjects.reduce(function(result, current) { return Object.assign(result, current); }, {}) but because it’s accumulating an object, it’s just override object properties with the same keys. Please help.