In my current project, I am dealing with a complex array of arrays where each inner array contains objects.
My goal is to transform this multidimensional array into a flat array organized in a column-wise order. Here is the code snippet that I have implemented so far:
const newArray = [];
arr.map((rows, i) => {
rows.map((row) => {
newArray.splice(i, 0, row);
});
});
However, as I progress further into the operation, the order starts getting jumbled up.
(Please note that I am using a Promise library within Node, so the maps should be treated as normal maps)
Current Array Structure
const arr = [
[
{ Mapped_Index: 'Alignment', item_1: '-' },
{ Mapped_Index: 'Alignment', item_2: '-', item_3: '-' },
{ Mapped_Index: 'Alignment', item_4: '-', item_5: '-' },
{ Mapped_Index: 'Alignment', item_6: '-', item_7: '-' }
],
[
{ Mapped_Index: 'Autonomy', item_1: '-' },
{ Mapped_Index: 'Autonomy', item_2: '-', item_3: '-' },
{ Mapped_Index: 'Autonomy', item_4: '-', item_5: '-' },
{ Mapped_Index: 'Autonomy', item_6: '-', item_7: '-' }
],
[
{ Mapped_Index: 'Belonging', item_1: '-' },
{ Mapped_Index: 'Belonging', item_2: '-', item_3: '-' },
{ Mapped_Index: 'Belonging', item_4: '-', item_5: '-' },
{ Mapped_Index: 'Belonging', item_6: '-', item_7: '-' }
]
]
Desired Flattened Array
const arr = [
{ Mapped_Index: 'Alignment', item_1: '-' },
{ Mapped_Index: 'Autonomy', item_1: '-' },
{ Mapped_Index: 'Belonging', item_1: '-' },
{ Mapped_Index: 'Alignment', item_2: '-', item_3: '-' },
{ Mapped_Index: 'Autonomy', item_2: '-', item_3: '-' },
{ Mapped_Index: 'Belonging', item_2: '-', item_3: '-' },
{ Mapped_Index: 'Alignment', item_4: '-', item_5: '-' },
{ Mapped_Index: 'Autonomy', item_4: '-', item_5: '-' },
{ Mapped_Index: 'Belonging', item_4: '-', item_5: '-' },
{ Mapped_Index: 'Alignment', item_6: '-', item_7: '-' },
{ Mapped_Index: 'Autonomy', item_6: '-', item_7: '-' },
{ Mapped_Index: 'Belonging', item_6: '-', item_7: '-' }
]