-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第60天 请实现一个flattenDeep函数,把多维数组扁平化 #252
Comments
// const newArray = arr.flat(depth)
const temp = [1,2,[1,2]];
console.log(temp.flat(Infinity));
// [1,2,1,2] |
|
function fn(arr) { return res |
function flattenDeep(arr) { |
|
基础数据:arr.toString().split(","); |
function flatten (arr) {
return arr.reduce((pre, current) => {
return pre.concat(Array.isArray(current) ? flatten(current) : current)
}, [])
} |
const flat = arrs => arrs.reduce((i,j) => Array.isArray(j)?[...i,...flat(j)]:[...i,j]) |
const deepFlatten = (arr) => {
let result = [];
arr.forEach((item) => {
Array.isArray(item)
? (result = [...result, ...deepFlatten(item)])
: (result = [...result, item]);
});
return result;
};
const arr = [
1,
[2, 3, "a", ["b"], ["c", "d", "e"]],
[4, 5, 6, [7, 8, 9, [10, 11, 12]]]
];
console.log(deepFlatten(arr)); |
Array.prototype.flattenDeep = function(layer = 3) {
return this.reduce(
(pre, curr) =>
Array.isArray(curr) && layer > 0
? [...pre, ...curr.flattenDeep(layer - 1)]
: [...pre, curr],
[]
)
}
console.log([1, 2, 3, [4, 5, [6, 7, [8, 9, [10]]]]].flattenDeep(2))
// [ 1, 2, 3, 4, 5, 6, 7, [ 8, 9, [ 10 ] ] ]
console.log([1, 2, 3, [4, 5, [6, 7, [8, 9, [10]]]]].flattenDeep(+Infinity))
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] |
// 扁平化
function flattenDeep(arr) {
let newArr = []
for (let i = 0; i < arr.length; i++) {
if (!Array.isArray(arr[i])) {
//当前元素不是数组则直接push
newArr.push(arr[i])
} else {
//当前元素是数组则递归自身返回一维数组拼接
newArr = newArr.concat(flattenDeep(arr[i]))
}
}
return newArr
}
let arr = [[0, 1, 2, 3, 4], 5, [6, 7, [8, 9]]]
console.log(flattenDeep(arr)) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
function flatDeep (arr) {
return arr.reduce((total, cur) =>
total.concat(Object.prototype.toString.call(cur) === '[object Array]' ? flatDeep(cur) : cur)
, []);
} |
const flat = arr => arr.toString().split(',') |
function arrFlat(arr, res) {
res = res || []
for (key in arr) {
if (arr[key] instanceof Array) {
arrFlat(arr[key], res)
} else {
res.push(arr[key])
}
}
return res
}
var arr = [1, 2, 3, [4, 5, 6, 7,[8,9,10,11]],[12,13,14,15]]
console.log(arrFlat(arr)) |
// Array.prototype.flat
if (!Array.prototype.flat) {
Array.prototype.flat = function (num = 1) {
if (!Number(num) || Number(num) < 0) {
return this;
}
var arr = [];
this.forEach((item) => {
if (Array.isArray(item)) {
arr = arr.concat(item.flat(--num));
} else {
arr.push(item);
}
});
return arr;
};
} |
涨知识了😁 |
使用 reduce、concat 和递归展开无限多层嵌套的数组var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flatDeep(arr, d = 1) {
return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
: arr.slice();
};
flattenDeep(arr1, Infinity); |
function flattenDeep(arr) {
return arr.reduce((acc, cur) => {
const _val = Array.isArray(cur) ? flattenDeep(cur) : [cur]
return [...acc, ..._val]
}, [])
}
console.log(flattenDeep(
[
1, 2, 3, [4, 5, [6, 7, [8, 9]]]
]
)) |
` <title>Document</title>
|
function flattenDeep(arr) { |
const flat = (arr) => {
const newArr = [];
for (let i = 0; i < arr.length; i++) {
if (!Array.isArray(arr[i])) {
newArr.push(arr[i]);
} else {
newArr.push(...flat(arr[i]));
}
}
return newArr;
} |
第60天 请实现一个flattenDeep函数,把多维数组扁平化
The text was updated successfully, but these errors were encountered: