Example data set
const data = [
{
location: "1A",
uId: 1,
notNeededData: null,
components: [
{
modelId: "7654",
partNumber: "P1",
description: "It's a desk.",
notNeededData: null,
location: "office1"
},
{
modelId: "1234",
part: "P2",
description: "It's a chair",
notNeededData: null,
location: "office1"
}
]
},
{
location: "2B",
uKeyId: 1,
notNeededData: null,
components: [
{
modelId: "9876",
partNumber: "P8",
description: "The best headrest",
notNeededData: null,
location: "office2"
},
{
modelId: "7463",
partNumber: "P5",
description: "The stool",
notNeededData: null,
location: "office2"
}
]
}
];
Desired result set as a new array of objects, as follows:
[
{
id:1,
uId: 1,
location: "1A",
modelId: "7654",
partNumber: "P1",
description: "It's a desk."
},
{
id:2,
uId:1,
location: "1A",
modelId: "1234",
part: "P2",
description: "It's a chair"
},
{
id:3,
uId: 2,
location: "2B",
modelId: "9876",
partNumber: "P8",
description: "The best headrest"
},
{
id:4,
uId: 2,
location: "2B",
modelId: "7463",
partNumber: "P5",
description: "The stool"
}
]
I have tried iterating through the array with the following function, but I only succeed in duplicating only a few value sets.
const getNewDataSet = (d) => {
let newArr = [];
for (let i = 0; i < d.length; i++) {
let obj = {};
obj["id"] = i + 1;
obj["uId"] = d[i].uId;
obj["location"] = d[i].location;
for (let k = 0; k < d[i].components.length; k++) {
obj["modelId"] = d[i].components[k].modelId;
obj["partNumber"] = d[i].components[k].partNumber;
obj["description"] = d[i].components[k].description;
newArr.push(obj);
}
}
return newArr;
};
Please let me know if there is any additional information required, or anything that I may have left out.
Greatly appreciated, thank you.