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})
})
}
})
})
CodePudding user response:
You could check if the entries of the object contains an array and reduce the entries or take the object for mapping.
const
data = [{ "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"] }],
result = data.flatMap(object => {
const entries = Object.entries(object);
return Array.isArray(entries[0][1])
? entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
: object;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Without flatMap
const
data = [{ "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"] }],
result = data.reduce((array, object) => {
const entries = Object.entries(object);
if (Array.isArray(entries[0][1])) return [
...array,
entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
];
return [...array, object];
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
You can use Array#reduce
and Object.keys
with Array#map
methods as follows:
const input = [
{
"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"
]
}
];
const output = input.reduce((prev, {Length, ...rest}) =>
Array.isArray(Length) ?
prev.concat( Length.map((l,i) => Object.fromEntries( Object.keys(rest).map(k => [k,rest[k][i]]).concat( [["Length",l]] ))) ) :
prev.concat( {...rest,Length} ), []
);
console.log( output );