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

Redirecting a webpage in JavaScript

What is 'this.' keyword in JavaScript

Introduction to webshare API (navigator.share)