0

I have this array:

arr = [[1,2,3,4,5],[3,5,6,8],[3,2,1,5,7,9,10,11],[3,5,6,8,2,1,3,4,6]]

I want it to become

arr = [1,2,3,4,5,3,5,6,8,3,2,1,5,7,9,10,11,3,5,6,8,2,1,3,4,6]

How can I do that?

DustInCompetent
  • 425
  • 2
  • 9
nlstmn
  • 167
  • 1
  • 10
  • 7
    Would `arr.flat()` not work? – evolutionxbox Feb 18 '21 at 10:38
  • 1
    Does this answer your question? [Merge/flatten an array of arrays](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) – evolutionxbox Feb 18 '21 at 10:41
  • [Title of your question on google search](https://www.google.com/search?q=Merge+arrays+inside+of+array+javascript+site%3Astackoverflow.com). The very first result is the duplicate. Please read: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/3082296) – adiga Feb 18 '21 at 10:47

2 Answers2

3

Use Array.flat()

const arr = [[1,2,3,4,5],[3,5,6,8],[3,2,1,5,7,9,10,11],[3,5,6,8,2,1,3,4,6]]
const output = arr.flat();
console.log(output);
William Wang
  • 8,051
  • 3
  • 5
  • 28
1

You need to use Array.flat() method to merge arrays inside of array.

class Test extends React.Component {
  render() {
    const arr = [[1,2,3,4,5],[3,5,6,8],[3,2,1,5,7,9,10,11],[3,5,6,8,2,1,3,4,6]];
    
    return console.log(arr.flat());
  }
}

ReactDOM.render(<Test />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>
Raeesh Alam
  • 2,257
  • 1
  • 7
  • 6