Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Flattening an Array

Flattening an array involves reducing its dimensionality.

This process is beneficial when converting a multi-dimensional array into a one-dimensional array.

JavaScript Array flat()

ES2019 introduced the Array flat() method.

The flat() method generates a new array by concatenating sub-array elements up to a specified depth.

Example

const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();

Browser Support

The JavaScript Array flat() method is supported in all modern browsers as of January 2020.

IMG_4150

Splicing and Slicing Arrays

The splice() method inserts new items into an array.

Meanwhile, the slice() method extracts a portion of an array.