flatten array in javascript without flat code example
Example 1: js convert array of arrays to array
// Converts array with multiple values into a single array with all items:
var merged = [].concat.apply([], arrays);
Example 2: how to flatten nested arrays javascript
// using recursion, .reduce() and .concat() methods
// works with arrays of any depth
function flatten(arr)
{
return arr.reduce((acc, cur) => acc.concat(Array.isArray(cur) ? flatten(cur) : cur), []);
};
const arr = [[1,2],[3,[4,[5]]]];
const flattened = flatten(arr);
console.log(flattened);
/*
Output: [ 1, 2, 3, 4, 5 ]
*/