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 :: js get english alphabet 
Javascript :: react router dom change default path 
Javascript :: generate random 6 digit number javascript 
Javascript :: get last letter of string javascript 
Javascript :: javascript min max array 
Javascript :: firebase auth api key not valid. please pass a valid api key 
Javascript :: check / unchecked a checkbox with jQuery 
Javascript :: extract value from object javascript 
Javascript :: get zipcode from google places autocomplete 
Javascript :: javascript add event listenner for multiple events 
Javascript :: javascript image to blob 
Javascript :: regex street 
Javascript :: http delete angular 
Javascript :: how to get array from object in javascript 
Javascript :: hypot in javascript 
Javascript :: rails to json 
Javascript :: find text in label jquery 
Javascript :: js for 
Javascript :: nodejs read image as base64 
Javascript :: Get element id by name 
Javascript :: react native password strength meter 
Javascript :: innertext of element js 
Javascript :: how to get first and last element of array in javascript 
Javascript :: js check if string is int 
Javascript :: react native "modalize" above bottom navigation 
Javascript :: delete row in html table using javascript 
Javascript :: javascript get fibonacci number 
Javascript :: how to use platform.select 
Javascript :: magento 2 translate js 
Javascript :: window scroll top 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =