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 wait for a promise within a loop block

How to use canvas context rect method for drawing rectangles

How to interpolate strings in Kotlin programming language

How to get user's ip address in JavaScript

Ways to iterate array items in JavaScript