JavaScript tutorial - array.prototype.flat()

JavaScript tutorial - array.prototype.flat()

This tutorial covers array.prototype.flat() method. A multiple examples have been provided. Javascript Array flat() is an inbuilt method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It returns a new array with the sub-array elements concatenated into it.

Javascript Array flat() is an inbuilt method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It returns a new array with the sub-array elements concatenated into it.This tutorial covers array.prototype.flat() method. A multiple examples have been provided.

Javascript Array Flat Example

As its name suggests, the Array flat() method available on an Array prototype returns the new array that’s the flattened version of an array it was called on. If we do not provide arguments passed-in, then the depth of 1 is assumed.

The syntax of the Javascript Array flat() method is the following.

let newArray = arr.flat([depth]);

The depth level is specifying how deep a nested array structure should be flattened. Defaults to 1. It is an optional parameter.

Let’s see the following example.

// app.js

const arr = [['krunal', 'ankit'], [21, 74]];

console.log(arr.flat());

See the output.

Javascript Array Flat Example | Array.prototype.flat() Tutorial

In the above example, by default, the depth level is 1. Let’s see the example where depth level is more than 1.

// app.js

const arr = [['krunal', 'ankit'], [21, 74], ['appdividend', ['Laravel', 'Angular']]];

console.log(arr.flat(2));

javascript array prototype flat programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Javascript Array Flat Example | Array.prototype.flat()

Javascript Array flat() is an inbuilt method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Javascript Array From Example | Array.prototype.from()

Javascript array from() is an inbuilt function that creates a new, shallow-copied array instance from an array-like object or iterable object.

JavaScript tutorial - array.prototype.flatMap()

Javascript Array flat() is an inbuilt method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It returns a new array with the sub-array elements concatenated into it.This tutorial covers array.prototype.flat() method. A multiple examples have been provided. This tutorial covers array.prototype.flatMap() method. This method is essentially a combination of map and flat methods. A multiple examples have been provided.

Javascript Array Shift Example | Array.prototype.shift()

Javascript array shift() is an inbuilt function that removes the first item from an array and returns that deleted item. It changes the length of the array.

JavaScript Array Some Example | Array.prototype.some()

JavaScript array some() is the inbuilt method that tests whether at least one item in the array passes the test implemented by a provided function. The some() method is the JavaScript extension to the ECMA-262 standard; as such, it may not be present in other implementations of the standard. JavaScript array some() is the inbuilt method that tests whether at least one item in the array passes the test implemented by a provided function. It returns the Boolean value.