0
How to iterate this array in JavaScript
let ar=[["as","vd",["dg","gd"]]];
1 Resposta
0
1. You can use lodash which a modern JavaScript utility library.
https://lodash.com/docs/4.17.15#flattenDeep
2. Also you can use JavaScript array prototype
Array.prototype.flat()
3. as the last, you can make it yourself using recursion.
function flatten(ary) {
var ret = [];
for(var i = 0; i < ary.length; i++) {
if(Array.isArray(ary[i])) {
ret = ret.concat(flatten(ary[i]));
} else {
ret.push(ary[i]);
}
}
return ret;
}
flatten([[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]]) // [0, 1, 2, 3, 4, 5]