I am currently working on expanding an array in JavaScript. Here is the object below
const items = [
{
id: 1,
name: 'Alice',
designs: [
{
designId: 1,
designName: "designA"
},
{
designId: 2,
designName: "designB"
}
]
},
{
id: 2,
name: 'Bob',
designs: [
{
designId: 3,
designName: "designC"
},
{
designId: 4,
designName: "designD"
}
]
},
{
id: 3,
name: 'Carol',
designs: []
},
];
[
{ id: 1, name: 'Alice', designId: 1, designName: 'designA' },
{ id: 1, name: 'Alice', designId: 2, designName: 'designB' },
{ id: 2, name: 'Bob', designId: 3, designName: 'designC' },
{ id: 2, name: 'Bob', designId: 4, designName: 'designD' },
{ id: 3, name: 'Carol', designId: null, designName: null },
]
Although it can be done using nested loops, I aim to accomplish this with higher-order functions.
Below is my code snippet
for (let i = 0; i < items.length; i++) {
for (let j = 0; j < items[i].designs.length; j++) {
const id = items[i].id
const name = items[i].name
result.push({
id,
name,
designId: items[i].designs[j].designId,
designName: items[i].designs[j].designName
})
}
}
Additionally, any insights on performance differences between nested loops and higher-order functions would be greatly appreciated.