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!

4
  • 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)? Commented Jan 4, 2021 at 2: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. Commented Jan 4, 2021 at 2:17
  • Does this answer your question? Merge/flatten an array of arrays Commented Jan 4, 2021 at 3:42
  • this question has been asked before Commented Jan 4, 2021 at 3:42

2 Answers 2

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));

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.