I have this array:
[
{
"number": 1,
"detail": detail1,
"value": [1, 11, 21, 31]
},
{
"number": 2,
"detail": detail2,
"value": [2, 12, 22, 32]
},
{
"number": 3,
"detail": detail3,
"value": [3, 13, 23, 33]
}
]
And I need it in this form:
[
[
{detail: detail1, value: 1},
{detail: detail2, value: 2},
{detail: detail3, value: 3},
],
[
{detail: detail1, value: 11},
{detail: detail2, value: 12},
{detail: detail3, value: 13},
],
[
{detail: detail1, value: 21},
{detail: detail2, value: 22},
{detail: detail3, value: 23},
],
[
{detail: detail1, value: 31},
{detail: detail2, value: 32},
{detail: detail3, value: 33},
],
...
]
How I could do this? Thanks for help
I tried to do it with loop the way shown below, but couldn’t get those form:
const result = []
for (let i = 0; i < array[0].value.length; i++) {
for (let j = 1; j < array.length; j++) {
for (let k = 0; k < array[j].value.length; k++) {
result.push(values)
}
}
}
Hope, I provided enough info
EDIT:
I changed question to improve array readability
4
Answers
this should do it.
We initialize an output array where we will be pushing the expanded forms of the individual objects.
At each ‘i’ object, I’m pushing an empty array to the output array. Then at each ‘j’ element in value, I’m pushing the new expanded object to the empty array.
You can use map and foreach with descructuring to transform your data:
Does the following help? Please see comments for a little info on what this is doing.