showing results for - "flatten array"
Irvin
21 Apr 2017
1// flat(depth), 
2// depth is optional: how deep a nested array structure 
3//		should be flattened.
4//		default value of depth is 1 
5
6const arr1 = [1, 2, [3, 4]];
7arr1.flat(); 
8// [1, 2, 3, 4]
9
10const arr2 = [1, 2, [3, 4, [5, 6]]];
11arr2.flat();
12// [1, 2, 3, 4, [5, 6]]
13
14const arr3 = [1, 2, [3, 4, [5, 6]]];
15arr3.flat(2);
16// [1, 2, 3, 4, 5, 6]
17
18const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
19arr4.flat(Infinity);
20// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Jaiden
12 Jun 2017
1var arrays = [
2  ["$6"],
3  ["$12"],
4  ["$25"],
5  ["$25"],
6  ["$18"],
7  ["$22"],
8  ["$10"]
9];
10var merged = [].concat.apply([], arrays);
11
12console.log(merged);
Kenny
26 Jan 2017
1var multiDimensionArray = [["a"],["b","c"],["d"]]; //array of arrays
2var flatArray = Array.prototype.concat.apply([], multiDimensionArray); //flatten array of arrays
3console.log(flatArray); // [ "a","b","c","d"];
Hannes
20 Aug 2019
1// Although this now may be an older version of how to faltten an 
2// array of arrays. I still want to post it so some may have an understanding 
3// of how it works
4
5function falltenArray(arr) {
6  
7  let result = [...arr];
8  let flattened = false;
9  let counter = 0;
10  
11  while (flattened === false){
12	// checks to see if the element at the counter index is an array
13      if (Array.isArray(result[counter])){
14        // unpacks the current array element back into the array
15        result.splice(counter, 1, ...result[counter]);
16        // if so the counter should start at the beginning of the array
17        counter = 0;
18        
19      } else {
20        counter += 1;
21      }
22
23      if (counter === result.length){
24        flattened = true;
25      }
26  }
27  
28  return result;
29}
Griffin
16 May 2017
1const arr = [1, 2, [3, 4]];
2
3// To flat single level array
4arr.flat();
5// is equivalent to
6arr.reduce((acc, val) => acc.concat(val), []);
7// [1, 2, 3, 4]
8
9// or with decomposition syntax
10const flattened = arr => [].concat(...arr);
11
Melina
30 Jun 2016
1function flatten(arr) {
2  return arr.reduce(function (flat, toFlatten) {
3    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
4  }, []);
5}
6
queries leading to this page
javascript flatten a given arrayarray flattenflatten nnested arrayjs unfold arrayflaten array javascriptjs array of arrays to single arrayflatten in javascriptflatten an arraynode js flatten array 27flattent a arrayflat method javascriptjavascript array reduce concat stringflat an array javascriptflat in jshow to flatten an array in javascriptjavascript arra flatflatten an array within an arrayflat arrqaycollect array elements into flat array javascripthow to flar map jsarray flat javascriptextrans nested array and flat in javascriptadd array of arrays into one array javascriptexpand array of arrayflat arrays in js using reduceflatten arrflatten jsexpand nested array javascriptjs make array flatflat javascript arrayflatten arrays jsfaltten an array javascriptjs array flat 28 29javascript convert array of deptharray flat arrayjs join nested arrayjs concat nested arrayses6 flattenflatten js arrayalternative of flat in javascriptflatten array of array jsjs how to flatten an arrayhow to flatten array in nodejsjavascript array of arrays to one arrayhow to flatten array in javascriptflattend array of arraysarray flat 28 29 jsflatten aaray in javascriptarray of array to single array javascriptflatten array react jsjavascript flatten array w3schoolsflat javascriptwhat is to flaten an array jses6 nesterarrayhow to flatten an array javascriptjs array flat nodehow to flatten an arr jshow to flatten an arrayreduce arrays inside array javascriptflatten array in arrayflatten js object keysmdn array flatarray flat infinityarray flat dev firefoxflat all array of arraymake array of arrays a single array javascriptflat 28 29 3bflattening and arrayjs flatten 28 29how to flatten an array of arrays in javascriptflattern array jsflat array of arrays javascriptflatten nested array javascriptjs flat arrayarray of arrays into one arrayarray faltflat infinity jsarray flattenjs flatten alljavascript array flatten onlineplain arrays javascript flatjs collaspse arrayjs array flathow to make array of array to one array jsarray of partical array flatten js flatten javascript mdnjavascript array of arrays into single arrayflat array inside arrayarray flatten tses6 nested arrayflatten 2d array javascriptflat the array in jsflattenarray in javascriptjs flatten 23args flat 28 29 javascriptarray flat mapconver array of arrays to single array javascriptflat javascript mdnflatening an array in jsy jsflatten arraflaten arrayses6 flatten object of arrayscan i use flatjs flatten array of objects and arraysflatter array jsfrom array 2c return array of arrays with each item javascriptjavascript flatten an arrayjs unwrap array if single elementconvert array of array to array in jsdeep flatten array jsjs flatten the arrayarray flating in jsarr reduce to concat arraysget flat array from array of arrayshow to flatten nested arrays javascriptjavascript reduce 2d array to 1d arrayconvert nested array to single array javascripthow to write flatten function from scratch javascriptjs merge array of arrays into onejavascript flatten nested arraysmdn flatflatten a array javascripthow to flatten array javascriptflatten array of onangular flat joinconvert nested arra y t o a single array in jsflatten array javascript function flat function javascriptflattendeep javascriptjs flat by keyarray flat mdn flat 28 29 lengthjavascript flattern arrayjavascript arr fattenflatten array of arrays with javascriptjs spread array into array instead of nesting arraysarray of arrays to single array javascriptflatten a nested array javascript 5b1 2c2 2c 5b3 2c4 5d 2c 5b 5b5 2c6 2c7 5d 5d 5d js to flatpush flat in reduce javascripthow to flatten a nested array in javascriptalternative to flat 28 29flat array angularjs array flatten whyflatten array nodejavacsript flatten arrayflat an array javsacriptflatten arrayssjavascript array as item of array flattencollapse arrays within an arrayhow to flatten an array of arraysflatten an array javascripthow to flat an array in javascriptflat push arrays jsjavascript flatten array of arrays in single arrayarray flat javadcriptjs flatten functioinwhat does flattening an array mean 3farray flat javascript mdnarray javascript flattenjavascript convert array to flat objectarray of object to flat array js es6falttening an array programmingconcat all nested array to onereact flatten arrayhow to flatten a 2d array javascriptflatten array of ararys jswhat 27s flatten in javascriptflatten an list of arrays in javascriptarray flat infinityjavascript flatten list of listhow to reduce 2d array to one array javascripthow to flat the array object in javascriptinfinity flat 28 29node flattenhow to flat array of arrays angularjavascript concat nested arrayflattening an array javascripthow to flatten an array of arrays javascriptjs flatt arrayflatten in jsdeep flat array javascriptreduce array of arrays into separate variables javascriptflatten nested array in javascriptjavascritp flatten an arrayujs flattern arrayjs flatten array 21gwhat does flatten method in javascript dojs flatten arayflat on arrayreduce multiple arrays into oneflattening array of arrays javascript using isarray 28 29js flatten subarrayflat 28 29 in javascriptflatten arrays within arraysnode flat array of arraysdocsjavascript flatten arrayjavascript flatten outter array by one leveljs flattenjavascript flatten array and mergejs arrray flatflatten array of arrays in javascriptjs flatten araryflat 28infinity 29flatten infinite javasriptshow flat an array jsflattening an array by concatflattening an array in javascriptconvert a nested array into a flat array flat an array in javascriptarray flat jsmake array from array of arraysarray of arrays javascript flattenconcat and flat multi diminsional arraycombine array of arrays jsflat array nodejsjs 2c flatusing spread to flatten array of arrays javascript flat javascriptjavascript flatten array one levelflatten the array in javascriptflattening out an arrayarray of partical array flattenflat in array flat mdnhow to flatten an array of arrays javascript es6es6 array flat 5cflaten arrayarray metodo flatcombine nested arrays javascriptarray flat developer firefoxmake nested array one arrayjoin array of arraysflattens in jsflatten array javascriptarry flatjs arrays flatflatten array in javascriptjavascript flatjs flatten 2d arrayhow to flatten and array in jsflat arrays on javascri c2 b4tjavascript how to read flast array 3farray flatjavascript flatten arrayjavascript reduce multiple arrays to oneflat array in javascriptjoin an array of arrays into one arrayhow can i flatten a array in javascript 3fjs flatten arrjs array how to flatts flatten object to arrayarray of array js to one arrayarray flatten 28 29 jsjs array of arrays flattenflatten array javascript spreadflatten array concatarray flatten es6how to use reduce to merge array of arraysarray flat alternative lodashjavascript how to flatten an arrayjavascript array of arrays to single arrayflatten javascriptflat infinity in javascriptflat method in javascriptjs flatten arrytypescript array flatflattenn the arrayjavascript flat methodjavascript array unwrapflat out an array in jsflatten two arraysjavascript flatten array with one value convertflatten array nodejsflatten merge arraysjs concat array of arraysflatten a array of arrys jsflatten arrays javascriptjavascript array of array flattenhow di i flatten a 2d array jsflatten a javascript arrayhow to use flatten jsflat array google scriptjs flatten array of arraysarray flatten objectsjavascript flaten array and access the keyhow to flatteb list in javascriptjs flat aaray of arraysjs array merge with flattennested arrays javascript in onejoin array of arrays into arrayjs array flat infiniteflatten multidimensional array javascriptjavascript how to flatten arraynested array to one array javascriptarray flat es6array 23flat 28 29js flatten array one leveljavascript flatern array flat 28 29 2b javascriptjs flatten array of arrays into arrayjavascript string array to flat arrayarray of arrays to array javascriptjavascript put child arrays in their own arrayflat the arrayjavascript array flat functionjs array flat alljavascript flatten 28array 29javascript array flatten array objectsflat an array in javascirptwhat does flattening the array meanjs array fla all sub elementswhat is array flattening in javascriptflatten angular 8arr flat 28js flatarrayflatte arrayarray flatten in javascriptnested array to single array typescriptflattrwen array jsarray 3a 3aflatterflatten array of arrayhow to flat array javascripthow to flatten a 2d array in jsflatten arryflattening array of arrays javascriptflat list jsjavascript array flatten objectsarray flatenarray flatten jsarray flatflat an arrayflat js arraynode js array inside array into single arraymerge nested arrays jstypescript flatten array of arraysflatten js functionjs flat functionjs flat 28 29flat inifint jsflatten array js vs forjavascript flatten one layer flat method angulararrays flattenjavascript flatten embedded arrays to array of objectsarray of arrays flatten javascriptjs toflathow to make array in array flat javascriptturn array of arrays into one array javascriptjs flatflat in javascriptflat array in anfulararray of array to one arrayflattening of array javascriptflatten an array flat arrayhow deep does array flat gojavascript flattan array of arraysflatten an array in javascriptjs array prototype flattenjavscript array of object flatjs flattebjavascript flat sub arrayhow to flatten a 2 dimensional array in javascript using reduce 28 29array of arrays join jsflatten a arrayjavascript flat array functionjs conver nested array into arrayflatten get values in node flat method es6 or es7flat 28 29 javascriptflatten an array of arrays javascriptchanges object to array flattent data javascriptjavascript flat related functionhow to flatten an array in javascript wothout flatenhow to flatten collection with arrayes6 flatflatten a lsit of lists javascriptjs flatten filter resultmdn flatjavascript flatten nested arraywhy flatten an arrayflatten for each javascripthow to flatten arrayflattening array javascriptarray to array of arrays javascripthow to flatten arrays inside arrays jsjavascript array flat allflatten array or arrays javascriptarrays flattenflatten array jsalgorithm behind flat in jsflatten arrahow to flatten an arryaconverting array of array into array jsjavascript 2b nested array into single arrayjs 2cflat flat in jsjs 2c flaten an arrayflat 28 29flattening a array of arraysjs flat 28 29 flat 28 29java script flatlist flat javascriptarray flatternhow to unnest an array in javascriptflatten two dimensional array javascriptjavascript reduce myflattenhow to flatten a aeray in javascripthow to make nested array in to one arrayflatten the array in jsflatten array in jsflat jsflatten array javascript es6 operator js array flatunwrap array javascriptjoin nested arrays javascriptcollapse an array to smaller items jsjavascript flat the array until it 27s flatnodejs flatten arrayjs flat array of two arraysjavascript flattenflat elements in array javascripthow to flat an array in javascript without using flat functionflattening array of arrays jsjs make flatten functionflat functions in javascriptjavascript array to array of arraysarray flatten javascriptflatten array 28 29javascript flat functionwhy is array flat not working in nodehow to return a flatten arrayflatten two arrays javascriptflattening the arrayjs flatten ajavascript flatten all arraysjavascript array or arrays unnestflatten the javascript arrayoposite of array flatjavascript flatten array functionflaten a nestes array of objectsjavascrip flathow to flat array in javascriptjs flattenrarray flatten jshow to flaten a 2d array javascripthow do i flatten an arrayextract outer array in javascriptflatten array function jsflatten arrays in arraysflattening a list in javascriptjs array index not flathow to flatten an array of strings in jsflatten arrayflat and reduce in javascriptarray flat combinesarray flat methodflat a mp array of arrays jsflatten array of arrays es6array and ovject flatten codejavascript flatten 2d arrayjs convert 2d array to 1dflatten function javascript ts flatten numberflatten an array of arraysjavascript flat arrayarray flatflattening array jswhat is flatten an arrayflatten arary jsmake a flat array from array of array listflattening a nested arrayjs array flatflatten list javascriptarray in array flatjavascrip flatten arrayjavascript build array from flatmap2 array prototype flat 28 29flettern arraymdn flat 28 29flatten araynode js array flatgiven an array of arrays 2c flatten them into a single array flattening javascript arrayflatten an array in jswhich method do you prefer to flatten an array 26 why 3f one 3a arr flat 28 29 3b two 3a 5b 5d concat 28 arr 29 3b three 3a arr reduce 28 28acc 2c val 29 3d 3e acc concat 28val 29 2c 5b 5d 29 3bflatten array in javascript es5javascript make flat arrayflatten array in es6 without inbuilt functionsarr flat 28infinity 29 javascriptreduce multiple arrays into one javascriptflattened arrray javascriptflatten nodehow to flatten a nested array javascriptjavascript array flatejavascript how to flatten a nested arrayflatten array 2cflatten array of array in jsflatten list jsjs collapse arrayflatten node listarray flat vs concat javascriptways to flatten an array jsflatten javascript arrayflatten list of lists javascriptfaltten an arrayflatern js arrayes6 flatten array of 150 arrays javascriptconcat array flattenjs concatenate array of arraysnode flattenarray flat valuejoin array of arrays es6use of flat in javascriptspread all arrays inside an arrayflat array javascriptobject values 28 29 flat 28 29node js array flattenjavascript flatarray flat 28 29 method in javascriptmap reduce flatten javascripthow to fltten an arrayhow to unnest an array with arrays javascriptnode flat arrayflat function in javascriptarray flattetninghow to flatten js arraynode flatted arrayflatten array within arrayflatten the array javascriptflat js infinitenode flatten array7flatten array functionflatten array js with constant spaceflatten the arrays jshow to flatten a 2 dimensional array in javascriptjavascript join all nested arrayjavascript unnest arrayflttern aray of arrays using map javasxeri c3 a5tarray flatteningarray flat nodejsflatten a nested array in typescriptflatten an array of array of numbershow to flatten an array jshow to flatten array of arrays javascriptjavascript best way to latten arrayarray flattening concept in javascripthow to flatten the arrayflatter javascriprhow to flatten collection with arraysjavascript faltten arraywhat does it mean by flatten arrayhow to flatten a arraynode array flatjs flatten arrayjavascript combine array of arraysflatarray fromhow to flat the array javascriptflatten array node js flatten javascriptarray flattenjavascriptflat array jshow to flaten aarrayflatten array es6flatten arr jsturn array of arrays into one arrayflatten array with flatflatten array of arraysarray in array to one array jsjavascript flatten array of objectarray prototype flathow to create array flatten function javascriptflattening nested array javascriptflatten raay in jsflat array methon in javascriptflattening an arrayhow do you flatten an arrrayjavascript flatten 28array of objects 29flatten array from on value in javascriptjavascript array of arrays flattenflattening array js implementationhow to flatten an array of objects in javascriptjs array of array flattenflatten array js without flathow to flatten a 2 d array in jsnested array to plain array javascriptmdn javascript array flatternfunction flatten jsflat the n dimenional array javascritjs flatten an array of arraysflat array to array of arrayflatten array a single level javascriptjavascrpt flatten flat 28 29 in javascript flat node jshow to flatten an array in js without an array function flat 28infinity 29 in javascriptjs flatten array w3flat i jsjavascript flatten array of objectsfaltten mdnmake array flat jsflatten tree to array javascripthow to flat array into one objectflatten array javascriptarray of arrays to one arrayjavascipt array flattenconvert deeply nested array to flat array javascripthow to flatten the array in javascriptflattening nested arrays javascriptjavascript array flatflat a arrayarray flat node jshow to make a flat array in javascriptjs array flat protoflatten example jsjavascript array flat 28 29javascript flatten array javascript isarrayjavascript concat array of arrays flat jsusing map to concat array of arraysflatten method in javascriptarray flatten js when to useflatten everything javscriptflaten array jsjs array flat methodjavascript es6 flatten arraywhat is array flatjavascript code for flat arrayturn an array of arrays into a single arrayjs flat multilevel arrayflatten 2d array jshow to flatten deep arrayhow to flatten multi level arrays javascriptnumpy flatten arrayarray of arrays flat javascriptflatten array of objects javascriptcollapse child arraysarray flat in jsjavascript array flat methodjoin array of arrays javascriptflat infinity javascriptconcat to flatten an arrayjs flat arraujs flat pushreact flat 28 29javascript array flatten 28 29js flat array of arrayssqaush array javascriptarray flat javascriptjavascript array flat alternativeflatten nested array in objectmdn flatten an arrayflat nodejsextra flat jsjs es6 flatten arrayjs flatten array of stringshow to flatten array javascript without flat 28 29multi level array flatten javascriptarray of array to arrayflatten array of arrays jcarray flatten mdnnode js flat arrayflatten array of arrays with keys javascriptjavascript array flathow to change array of arrays into single arrayjavascript flat array of arrayscombine array of arrays javascriptarray squashjavascript flatten array with one valueflat method in jsjs flatten an arrayflat an array in jsflattena rray jsarray flatten flat arrayflat array with hiralquyhow to flatten an array in javascript without flat 28 29js flatten object of arrayconvert array of arrays into single arrayflatter array of arrays javascriptflatten array fjavascript join nested arrayjs flatten array with flat falt javascriptflat array as objects mdnflatten an object to form an array jsfix the error 3a flattening an arrayflat infinity oesnt workhow to flat array or arrays angularmerge nested arrays javascriptflatten array in array javascriptarray of array to array javascriptflattening an array of array in javascripthotw to flat array of arrayflatten deep array flat method example in javascriptflatten array object javascriptjs flat array of array into oneflatten array attributes javascriptarray js flattenarr flat 28 29nested array and make into one javascriptes6 flatten 28array 29javascript flat array methodarray flat in javascriptjs array flattenconcat array of arrays jshow to flatten an array of arrayone array from array of arraysarray of arrays to arrayarray flat 28 29 javascriptarray prototype myflattenarray flatten eliminate nested and empty array return new array orderjavascript flat multidimensional arrayjavascript arr flattenjavascript array of arrays create arrayflat mdn jsjs array reverse concatflattening jsflatten arrays into one array javascript flatflat array of objects javascripthow to convert array of arrays into single arrayarray flatten eliminate nested and empty array return new array ordees6 flatten arrayflatten array methodjavascript flatten an array of arraysconvert array to flat array of key valuesarray of array to single arrayflatten arraysflatten jsjs array flattenflattening of arraynodejs array flatflatten es6javascript join array of arraysflat es6javascriopt flattenarray fllatjavascript list flattenflat array method javascriptjavascript array flattenflatten an array javascript without flathow to flatening array in javascripthow to flatten array of objects and arrays javascripthow to flatten array of objects javascriptjavascript flattenarrjs flaten listun flat array javascriptarray flatten 28 29flattening arrayconvert array of arrays to array javascriptflatten 28array 29 jsjavascript convert array of arrays to arrayconver array of arrays to single arraymake all arrays in an array into a single array javascriptflat the array of array into array in jsarray flatten javascriptbesto option to flatten an arrayjs flatten nested arrayflattening nested arraysts using flat on objects with arraysjavascript flatten array of arrays orderedflatteren in jscreate a single array from a nested arrayarray flat javascript definitionjavascript nested map flattenreduce to flatten array filterflat an array jsjavascript how to flatten a clamped arraflatten an array jsjs flatten nested arraysflatten javascript arrrayhow to flatten nested arraysarr flattenhow can i flatten array in javascript 3fflatten nested arraysjavascript make one array of arraysarray flat in javascriptjs flatnested arrays into a single array es6how to flatten js arrayjsflattenhow to fltten an array 2d jsjs flatten object of arraysjavascript best way to flatten arrayflattening array in javascriptmdn array flattennodejs group array 10 array flathow to make a flatten function from scratchjs flatten array in arrayflatten array of arrays javascriptflat array of arrayhow to flatten a 2 d array in js with flathow to flat array jsopen nested array one list javascriptjs flatten list of listsflatten 2 arrays in javascriptarray of array flatten jsflatten a nested arrayremove array from array javascript flatflaten an arrayjs array flat flatten whyjavascript unwrap single item in arrayflatenned arrayflatten 28 29 javascriptarray of array to one single arrayflatten array of arrays jsflat array infinite jses7 flatten arrayhow to flattern array of arrayshow to flatten an array in jsarray flatten function javascriptarray flat 28 29flat all array in array jsflat arrays es6flatten an array of objectshow do i flatten an array thats in an array in jsreduce multiple array with array prototypejavascript flatten array of stringshow to convert a nested array to a single array in javascriptjs how to flat arrflatten 28array javascript 29javascript flatten array of arraysflatten array javacripthow to turn a 2d array into a 1d array js flat methodarray flatten node jsarrar flattenflattening array using concat in javascriptarray methods javascript flatjavascript flatten array es6flat array in jsjavascript flatjs arrray flattennode js flatten arrayarray flat jsnested array make it single array javascriptcollapse array javascriptnested array to single array javascriptjs flat infinityflatten an array to a single array javascriptjs 2c flat 28 29one options array flat garna mil6js 2c flat 28 29 w3c flatten 28 29 typescriptjavascrtip flatten arrayarray flat js concatarr flatflatten array of array javascriptflatten array of 150 arrays javascriptjs array flattenjavascript array flatreact native flatten arrayflatten array javacdriptvue js array flatconcat array of arrays javascriptflatten array aof arry in jsarray of arrays flattenmake nested array flatten jsflaten arrays javascriptconvert array to flatflatten array javascript without methodflat out an arraymdn flatten arrayarray faltjavascript array flattennode flatten arrayunnest array javascriptflatten array of array itemsget all flat array javascriptreturn the flattened version of the arrayhow can i flat an array in javascript 3farray flatarr flat flat 28 29javascriptflattening arraysjavascript flaten array of arrayflatten matrix javascriptflatten array of satrings javascriptarray 5b 22flat 22 5dflattenarray jshow to retrun a flattend arrayarray flat es5javascript combine nested arraysjs flat array of array flat 28 29 javascriptflaten an array javascriptvalue flat jsjavascript flatten listes6 flatten array of arraysjaavscript flatten object of arraysflatten the arrayflat mdnflatten array