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 interpolate strings in Kotlin programming language

Animated sky on HTML5 Canvas

How to use fetch api submit an html form

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

How to install Kotlin programming language