Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

flatten array recursively

_.flattenDeep([1, [2, [3, [4]], 5]]);
Comment

how to flat an array in javascript recursively

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

function flatArray(arr) {

  return arr.reduce((flat, toFlatten) => flat.concat(
      Array.isArray(toFlatten) ? flatArray(toFlatten) : toFlatten), []);
}


function flatArrayIter(arr) {
  const tempArr = [...arr];
  const flat = [];

  while (tempArr.length) {
    
    const next = tempArr.pop();
    
    Array.isArray(next) ? tempArr.push(...next) : flat.push(next);
  }
  
  return flat.reverse();
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: bootstrap programmatically change tab 
Javascript :: javascript option yes/no popup 
Javascript :: suspense react 
Javascript :: how to use a fixed time zone in nodejs 
Javascript :: axios send file 
Javascript :: JavaScript Object Constructors 
Javascript :: detect iframe content change javascript 
Javascript :: get child element of parent by class 
Javascript :: js set iframe src 
Javascript :: localstorage setitem 
Javascript :: continuous scrolling js 
Javascript :: find and filter 
Javascript :: ok that is something 
Javascript :: jQuery hasClass() - check for more than one class 
Javascript :: remove duplicates from array of objects 
Javascript :: js platformer 
Javascript :: get window width 
Javascript :: get font size jquery 
Javascript :: js sum of int in array 
Javascript :: do and tap operator rxjs 
Javascript :: javascript generate random number 
Javascript :: js get class property 
Javascript :: joi unique validation 
Javascript :: how to make back button react 
Javascript :: validation select option jquery 
Javascript :: react append classname 
Javascript :: loading 
Javascript :: nuxt query params 
Javascript :: how to set emmet for jsx in visual studio code 
Javascript :: javascript check for null variables 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =