-
Notifications
You must be signed in to change notification settings - Fork 900
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
手写实现 Array.flat() #242
Labels
Comments
|
|
|
~function () {
Array.prototype.myFlat = function (depth) {
return this.reduce((accu, cur) => Array.isArray(cur) && depth > 1 ? accu.concat(cur.myFlat(depth - 1)) : accu.concat(cur), []);
}
Array.prototype.myFlat2 = function (depth) {
const res = [];
if (!Array.isArray(this)) throw new Error('params must be abn Array');
this.forEach(item => {
Array.isArray(item) && depth > 1 ? res.push(...item.myFlat(depth - 1)) : res.push(item);
});
return res;
}
let arr = [1, [2, [3, [4, [5]]]]];
let arr1 = arr.myFlat(3);
let arr2 = arr.myFlat2(3);
console.log(arr1);
console.log(arr2);
// [1, 2, 3, 4, [5]]
// [1, 2, 3, 4, [5]]
}(); |
let flatArr = [[1,2],[4,5],[[9,3,4],4]]; |
function flat(arr) {
while(arr.some(ietm => Array.isArray(ietm))) {
arr = [].concat([...arr]);
}
return arr;
} |
Array.prototype.flat = function(n) {
n = n === undefined ? 1 : n;
if (n < 1) return this;
let newArr = [];
for (let i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
newArr = newArr.concat(this[i].flat(n - 1));
} else {
newArr.push(this[i]);
}
}
return newArr;
} |
不用递归 Array.prototype.flat = function (depth = 1) {
let newArr = [...this];
while (depth--) {
for (let i = 0; i < newArr.length; i++) {
if (newArr[i] instanceof Array) {
newArr.splice(i, 1, ...newArr[i]);
}
}
}
return newArr;
}; |
function* flatten(array, dep = Infinity, curdep = 0) {
for (const item of array) {
if (Array.isArray(item) && curdep < dep) {
yield* flatten(item, dep, ++curdep);
} else {
yield item;
}
}
}
var arr = [1, 2, [3, 4, [5, 6, [7, 8, 9]]]];
const flattened = [...flatten(arr)];
console.log(flattened); |
|
Array.prototype.myFlat1 = function () {
return this.reduce((acc, cur) => {
return acc.concat(cur);
}, []);
};
const arr = [1, 2, [3, 4]];
console.log(arr.myFlat1());
Array.prototype.myFlat2 = function (depth = 1) {
const arr = this.slice();
return depth > 0 ? arr.reduce((acc, cur) => {
return acc.concat(Array.isArray(cur) ? cur.myFlat2(depth - 1) : cur);
}, []) : arr.slice();
};
const arr1 = [1, 2, [[3], 4]];
// 不知道目标数组有几层的话,可以使用Infinity使得数组完全扁平化
console.log(arr1.myFlat2(2));
function flatten(input) {
const stack = [...input];
const res = [];
while (stack.length) {
const next = stack.pop();
if (Array.isArray(next)) {
stack.push(...next);// ... 展开运算符拷贝了一个新的数组
} else {
res.unshift(next);
}
}
return res;
}
const arr3 = [1, 2, [3, 4, [5, 6]]];
console.log(flatten(arr3));
function* flat(arr, depth = 1) {
for (const item of arr) {
if (Array.isArray(item) && depth > 0) {
yield flat(item, depth - 1);
} else {
yield item;
}
}
}
const arr4 = [1, 2, [3, 4, [5, 6]]];
const flattened = [...flatten(arr4, Infinity)];
console.log(flattened); |
function flat(arr, deep) {
let res = []
if (deep === 0) return arr
deep--
arr.forEach(item => {
if (Array.isArray(item)) {
res = res.concat(flat(item, deep))
} else {
res.push(item)
}
})
return res
}
const arr = [
[1, [2, 3, 4], 5],
1,
[3, 4, 5],
[2, [3, [4, 5], 6], 7]
]
console.log(flat(arr, 2)) |
/**
* 手写实现 Array.flat()
* 接收一个深度depth
* 不改变原数组
* 返回一个新数组
*/
Array.prototype.myFlat = function (depth) {
if (depth <= 0) {
return this;
}
let result = [];
this.forEach((el) => {
if (el instanceof Array) {
result = result.concat(el.myFlat(depth - 1));
} else {
result.push(el);
}
});
return result;
};
const testArray = [0, [1], 0, [1, [2]], [1, [[3]]]];
console.log(testArray.flat(3));
console.log(testArray.myFlat(3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: