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

javascript array flat

var arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]

var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]

var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]
Comment

flatten an array javascript

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

console.log(merged);
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

js flatten

import { flatten } from "ramda";

const flattenArray = flatten([1, [2, 3]])
Comment

PREVIOUS NEXT
Code Example
Javascript :: unable to open file in target xcode react native 
Javascript :: ecmascript compose 
Javascript :: navigator.clipboard is undefined 
Javascript :: add char in specific index stirng javascript 
Javascript :: multiple value selected in select2 
Javascript :: linear gradient reactjs 
Javascript :: json javascript 
Javascript :: jquey body onload 
Javascript :: javascript onclick open url same window 
Javascript :: Vuejs trigger function on route change 
Javascript :: discord.js messageDelete 
Javascript :: month list javascript 
Javascript :: javascript iterate over object keys and values 
Javascript :: desable no unused vars in vue.js 
Javascript :: document.getElementByClass is not a function 
Javascript :: update file json trong javascript 
Javascript :: javascript expressions 
Javascript :: onclick on non button 
Javascript :: prime number js 
Javascript :: javascript autoscroll 
Javascript :: remove double slash from url javascript 
Javascript :: electron open new window 
Javascript :: url in js 
Javascript :: dom element get attribute 
Javascript :: placeholder value js 
Javascript :: discordjs eval 
Javascript :: check data type in javascript 
Javascript :: discord.js arguments 
Javascript :: jquery set att 
Javascript :: insert variable in string javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =