Today my brain seems to be in full-on "fart" mode. Struggling to find a clean solution for this task without resorting to an ugly loop or recursion.
I have a 2D array of numbers that looks like this:
const layoutMatrix = [
1,
[1],
[0.5, 0.5],
[0.2, 0.4, 0.4],
];
My goal is to convert this array into a new format where each entry is represented as an object with the value assigned to span
, and the index of each first-level array assigned to row
:
[
{ row: 0, span: 1 },
{ row: 1, span: 1 },
{ row: 2, span: 0.5 },
{ row: 2, span: 0.5 },
{ row: 3, span: 0.2 },
{ row: 3, span: 0.4 },
{ row: 3, span: 0.4 },
]
Prerequisites:
- This array will never have more than 2 levels, so there's no need for complex recursion.
- The values can either be single numbers or arrays of numbers.