0

I am trying to convert the following 3D array into a 2D array. Currently, my data has the following structure.

[ [[0,0,345], [1,0,555], ... [9,0,333]], ... [[0,9,1000], [1,9,987], ... [9,9,129]] ]

into

[[0,0,345], [1,0,355], ... [9,0,333], [0,1,1000], [1,1,987], ... [9,9,129]]

so the first element contains a width value, and the second is the height value. The third value will be a random value from 0 to 1023.

As you see, the width and height will be 10 each. And I am trying to get rid of the outermost array.

I have tried to iterate for each row to bounce to a new array using push, but keep getting undesired forms. Any help will be appreciated!

Muhammad
  • 6,725
  • 5
  • 47
  • 54
llamahahn
  • 11
  • 3
  • what do you want the comma to mean between the two array you list? or is your example wrong and you actually just want a flat array of "points" (three-value arrays)? – Christian Fritz Jan 04 '21 at 02:10
  • You were right @ChristianFritz, I have now edited. Each array means each sensor with its x and y value and pressure value at the end. I want all the sensors (so far we have 100) to be inside one mother array. – llamahahn Jan 04 '21 at 02:17
  • Does this answer your question? [Merge/flatten an array of arrays](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) – Christian Fritz Jan 04 '21 at 03:42
  • this question has been asked before – Christian Fritz Jan 04 '21 at 03:42

2 Answers2

1

You can directly use Array#flat.

const arr = [ [[0,0,345], [1,0,555], [9,0,333]],  [[0,9,1000], [1,9,987], [9,9,129]] ];
const res = arr.flat();
console.log(JSON.stringify(res));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

you can try this flatmap. Example below.

let data = [[["creative fee (example)"], [1000]], [["Item 1...", "Item 2..."], [600, 1000]], [["Item 3...", "Item 4..."], [400, 600]]],
    result = data.flatMap(([l, r]) => l.map((v, i) => [v, r[i]]));

console.log(result);
Ork Sophanin
  • 138
  • 6