Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Using flat() method

const arr1 = [1, [2, 3, 4], 5];
const flattened1 = arr1.flat();
console.log(flattened1); // [ 1, 2, 3, 4, 5 ]

const arr2 = [1, 2, [3, 4, [5, 6]]];

const flattened2 = arr2.flat();
console.log(flattened2); // [1, 2, 3, 4, [5, 6]]

const flattened3 = arr2.flat(2);
console.log(flattened3); //  [ 1, 2, 3, 4, 5, 6 ]

const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
const flattened4 = arr4.flat(Infinity);
console.log(flattened4); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

// flat() removes holes
const numArr = [1, , 3];
console.log(numArr.flat()); // [ 1, 3 ]
Comment

PREVIOUS NEXT
Code Example
Javascript :: service worker registration 
Javascript :: target child event 
Javascript :: js display image from external url 
Javascript :: what is javascript runtime 
Javascript :: max method in js 
Javascript :: js html object 
Javascript :: remove object from array of object 
Javascript :: prepen an element js 
Javascript :: event property value in angular 
Javascript :: react native api call 
Javascript :: hoisting in javscript 
Javascript :: javascript update value when slider moves javascript 
Javascript :: Sign in with Apple JS 
Javascript :: big o notation javascript 
Javascript :: Export multiple variable javascript 
Javascript :: pdfjs get all the text present 
Javascript :: Mqtt js react-native 
Javascript :: how to change array element to integer in js 
Javascript :: javascript iterate through an object 
Javascript :: javascript refresh element 
Javascript :: postgress express format 
Javascript :: (node:9130) electron: Failed to load URL: http://localhost:5000 
Javascript :: arithmetic operators in javascript 
Javascript :: javascript create object empty 
Javascript :: angular object sort by key 
Javascript :: react: fow to use find(to get the id of a element 
Javascript :: for loop js 
Javascript :: js tostring 
Javascript :: js number round to each 15 
Javascript :: scroll up own 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =