-
-
Save Integralist/749153aa53fea7168e7e to your computer and use it in GitHub Desktop.
const flattenTco = ([first, ...rest], accumulator) => | |
(first === undefined) | |
? accumulator | |
: (Array.isArray(first)) | |
? flattenTco([...first, ...rest]) | |
: flattenTco(rest, accumulator.concat(first)) | |
const flatten = (n) => flattenTco(n, []); | |
console.log(flatten([[1,[2,[[3]]]],4,[5,[[[6]]]]])) |
is there any alternative regardless flatten
I'm using the following, and it works like a charm:
export function flattenArray( array: Array<any> ): Array<any> {
const flattenedArray: Array<any> = [].concat( ...array );
return flattenedArray.some( Array.isArray )
? flattenArray( flattenedArray )
: flattenedArray;
}
You can perhaps use Array.prototype.flat()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
@bt4R9 your code has a bug. You can't just check if a value is truthy, you must check if it's undefined, otherwise the method stops processing. For example, flatten_loop([1,null,2])
will equal [1]
, not [1,null,2]
.
This version should fix that:
const flatten_loop = arr => {
let stack = [];
let item;
while ((item = arr.shift()) !== undefined) {
if (Array.isArray(item)) {
arr = item.concat(arr);
} else {
stack.push(item);
}
}
return stack;
}
var a = [1,2,3,[1,2,3],4,[5,6,7], 9,10] ;
var result = [];
a.forEach(data=>{
result = [...result].concat(data)
})
console.log(result);
Output
[1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 9, 10]
Not effective but interesting!
arr = [1,2,3,[4,5,[1,2,3,4,56,67],6,7,[7.5,7.6,7.9],8],9,10,11]
const flat = (arr, depth= Infinity ,arr2 = []) =>{
arr.forEach(e => {
typeof e === 'object' && depth ? flat(e, depth-1 ,arr2) : arr2.push(e)
});
return arr2
}
console.log(flat( arr, 1 )) // [ 1, 2, 3, 4, 5, [ 1, 2, 3, 4, 56, 67 ], 6, 7, [ 7.5, 7.6, 7.9 ], 8, 9, 10, 11 ] // Depth 1 falt
console.log(flat(arr)) // [ 1, 2, 3, 4, 5, 1, 2, 3, 4, 56, 67, 6, 7, 7.5, 7.6, 7.9, 8, 9, 10, 11 ] // Full flat
const deepFlat = (arr) => arr.flat(Infinity)
one hack solution. (deep nested also works)
JSON.stringify(arr).split(',').map(each => each.match(/[+-]?\d+(?:\.\d+)?/g)[0])
one hack solution. (deep nested also works)
JSON.stringify(arr).split(',').map(each => each.match(/[+-]?\d+(?:\.\d+)?/g)[0])
work like a charm for number, but not with string ['un', 2, 'trois', 4, 'cinq'].
For immutable solution, this should be the most performant one, because it uses only pop() and push():
const flatten = (list) => {
const stack = [list];
const result = [];
while(stack.length > 0) {
const elem = stack.pop();
if (Array.isArray(elem)) {
for (let i = elem.length - 1; i >= 0; i--) {
stack.push(elem[i]);
}
} else {
result.push(elem);
}
}
return result;
};
`/* flatten depth 1 */
let flatten = arr => [].concat(...arr);
/**