MeshWorld India LogoMeshWorld.

Identify whether device is a mobile or desktop

Listen to ArticleAI Speech
~2 min read narration
100%
Identify whether device is a mobile or desktop

In this article, we will see how to flatten an array using native javascript and also a glimpse of lodash(an open-source JavaScript utility library) _.flatten method.

During development, the developer needs to go through loops to prepare a single dimensional array out of a multi-dimensional array or flatten an array in JavaScript.

const detectDeviceType = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? ‘Mobile’ : ‘Desktop’;

// Example detectDeviceType(); // “Mobile” or “Desktop”

Array Flatten One Level Deep

Using reduce method

Now using reduce method

let nestedArray = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

// Native
const flatten = nestedArray.reduce((a, b) => a.concat(b), []);
console.log(flatten);
// Output => [12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]];

Using concat method

Now using concat method

let nestedArray = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

const flatten = [].concat(...nestedArray);
console.log(flatten);
// Output => [12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]

Using flat method introduced in ES2019

Now using flat method introduced in ES2019

let nestedArray = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

// Native(ES2019)
const flatten = nestedArray.flat();
console.log(flatten);
// Output => [12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]

Using flatMap method

Now using flatMap method

let nestedArray = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

const flatten = nestedArray.flatMap((number) => number);
console.log(flatten);
// Output => [12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]

Array Flatten Using Lodash/Underscore library

Now using _.flatten method from Lodash/Underscore utility library.

let nestedArray = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];
// Underscore/Lodash
_.flatten(nestedArray);
console.log(flatten);
// Output => [12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]

Infinitely Nested Arrays using flat method introduced in ES2019

Suppose you want flatten an array infinitely deep. We just need to pass Infinity within flat method introduced in ES2019.

const infiniteDeep = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

console.log(infiniteDeep.flat(Infinity));
// [12, 2, 4, 124, 7, 10, 8, 15, 6, 7]

This article is written in a ghostwriter.

Hope you like this!

Keep helping and happy 😄 coding

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive