JavaSript数组扁平化去重
2021-04-12 03:27
标签:array isarray 数组 log turn each set war bsp JavaSript数组扁平化去重 标签:array isarray 数组 log turn each set war bsp 原文地址:https://www.cnblogs.com/coderDemo/p/13356722.htmlfunction flattening(arr = []) {
let newArr = [];
arr.forEach(item => {
if (Array.isArray(item)) {
newArr.push(...flattening(item));
} else {
newArr.push(item);
}
});
return [...new Set(newArr)];
}
let a = [1, ‘ok‘, ‘ok‘, 2, [3, 4], [[[[[5, 6], 7]], 8], 1, 2, 3, 3]];
console.log(flattening(a));
// => [1, "ok", 2, 3, 4, 5, 6, 7, 8]