Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript flatten an array

let arr = [
    [1, 2],
    [3, 4],
    [5, 6][7, 8, 9],
    [10, 11, 12, 13, 14, 15]
];
let flatArray = arr.reduce((acc, curVal) => {
    return acc.concat(curVal)
}, []);

//Output:  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
Comment

flatten an array javascript

var arrays = [
  ["$6"],
  ["$12"],
  ["$25"],
  ["$25"],
  ["$18"],
  ["$22"],
  ["$10"]
];
var merged = [].concat.apply([], arrays);

console.log(merged);
Comment

flatten array

a = np.array([[1,2], [3,4]])
>>> a.flatten()
array([1, 2, 3, 4])
>>> a.flatten('F')
array([1, 3, 2, 4])
Comment

javascript array flatten

const flatten = (ary) => ary.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), [])
Comment

function for flatten an array

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

// to enable deep level flatten use recursion with reduce and concat
function flatDeep(arr, d = 1) {
   return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
                : arr.slice();
};

flatDeep(arr, Infinity);
// [1, 2, 3, 4, 5, 6]



// Faltten an array
const age=[1,2,
           [12,23,[75,34,[2,34]],[32,45]],
           [3,456],
           [56,5]]
// to store resultant array
let result=[]
function flattenArray(inputArray)
{
  inputArray.map((value)=>{
    if(Array.isArray(value))
    { 
      flattenArray(value)
    }
    else{
      result.push(value)
    }})
  }
// calling function to fallten an array
flattenArray(age);
Comment

array flatten

const arr = [1, 2, [3, 4]];

// To flat single level array
arr.flat();
// is equivalent to
arr.reduce((acc, val) => acc.concat(val), []);
// [1, 2, 3, 4]

// or with decomposition syntax
const flattened = arr => [].concat(...arr);
Comment

flatten array

function flatten(arr) {
  return arr.reduce(function (flat, toFlatten) {
    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
  }, []);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: manage nodejs versions on windows 
Javascript :: js add element to array 
Javascript :: .shift javascript 
Javascript :: nodejs append to json 
Javascript :: react recoil 
Javascript :: angular scroll to element horizontally 
Javascript :: nullish coalescing operator 
Javascript :: alertify js vue 
Javascript :: difference between normal function and arrow function 
Javascript :: javascript Set Difference Operation 
Javascript :: multi dimensional array javascript 
Javascript :: js array remove undefined values 
Javascript :: create neact native app 
Javascript :: javascript arreglos 
Javascript :: toast success 
Javascript :: mean stack 
Javascript :: mongoose use unified topology 
Javascript :: stdout javascript 
Javascript :: call a self executing function javascript 
Javascript :: get browser cookie 
Javascript :: how to get value inside span using javascript 
Javascript :: C# Convert Json File to DataTable 
Javascript :: how to see my timezone using js 
Javascript :: javascript get last word in string 
Javascript :: vue nested loop 
Javascript :: javascript clone object 
Javascript :: try catch js 
Javascript :: new date 
Javascript :: xmlhttprequest status codes 
Javascript :: replace char at index of string 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =