how to transform an array of arrays into a single javascript array?

Asked

Viewed 631 times

-1

Given the array: [[[1,2,3],[4,5,6]]

As Transformer in: [1,2,3,4,5,6]

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

  • I’m sure it’s duplicate, but I haven’t found the question that already exists.

  • @hkotsubo I suggest you post this in an answer. :)

  • @I think the duplicate that Anderson found already solves the problem. And the documentation that Linkei says is an experimental feature that may not work well on all browsers, so I don’t think it’s even worth an answer...

2 answers

4

you can do it this way.:

var matriz = [
  ['A', 'B', 'C'],
  ['D'],
  ['E', 'F'],
  ['G', 'H', 'I', 'J']
]

var array = matriz.reduce((list, sub) => list.concat(sub), [])
console.log(array)

2

var x = [["1", "2", "3"], ["3", "4", "5"]];    
var y = x[0].concat(x[1]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.