Sitemap
JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

ES2019. Flatten arrays in Javascript with Array.flat()

“Flatten arrays” thanks to the new proposal ES2019

3 min readSep 24, 2019

--

Today I bring you an article — recipe about Javascript that will help me to start talking about the news that ES2019 will bring and in which I will focus on how we can “flatten” arrays through Javascript.

So without entertaining much more, let’s see it!

But what is that about flattening?

Flattening arrays is a very useful operation that performs the following:

Press enter or click to view image in full size

That is, by passing an array of arrays, what it does is transform it into an array of a single dimension.

However, this operation is not implemented natively in Javascript, so it has been necessary to use some imagination (or Stackoverflow 😋) to obtain any possible implementation.

Some flatArray() implementations

With a simple Google search you will find many flatArray implementations. For example we have the following:

const merged = [].concat.apply([], arrays);

--

--

Gerardo Fernández
Gerardo Fernández

Written by Gerardo Fernández

Entre paseo y paseo con Simba desarrollo en Symfony y React

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.