Array concat method in JavaScript


Array concat is a method and an array property for concating one array onto another to archive merging of array elements.

Syntax for array concat;

const arr = [0,1,2,3]
const arrr = [4,5]
arr.concat(arrr)
// [0,1,2,3,4,5]

Merging nested arrays using spread operator.
It's possible to merge nested arrays to form a uniform array.

Take for instance you have two 2-dimensional arrays,
Or let's call them matrices

const arr2d = [[1,0],[0,1]]
//[[1,0],
// [0,1]]
const arrr2d = [[1,1],[0,1]]
//[[1,1],
// [0,1]]

For instance you want to reduce the two 2-dimentional arrays and create a new array instance;

[].concat(...arr2d.concat(...arrr2d))
// [1, 0, 0, 1, 1, 1, 0, 1]

Comments

Popular posts from this blog

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript