我正在尝试编写一个函数来展平数组。我有部分功能正常工作,另一半我需要帮助。
flatten: function(anyArray, singleLevel) {
if (singleLevel == true) {
flatArray = Array.prototype.concat.apply([], anyArray);
return flatArray;
}
flatArray = Array.prototype.concat.apply([], anyArray);
if (flatArray.length != anyArray.length) {
flatArray = someObject.array.flatten(flatArray);
}
return flatArray;
}
如果我输入
.flatten([[[1],[1,2,3,[4,5],4],[2,3]]], true);
我希望它只展平一个级别:
[[1],[1,2,3,[4,5],4],[2,3]]
答案 0 :(得分:5)
concat
array method需要一个或多个数组作为参数,其元素将被追加:
[1].concat([2, 3], [4]) // [1, 2, 3, 4]
因此,如果您使用的是apply
,那将会推平另一个级别:
[].concat.apply([1], [[2], [3]]) // === [1].concat([2], [3])
因此,您可以使用push
代替concat
,或call
(或直接调用)代替apply
,以获得仅一个展平级别。
答案 1 :(得分:0)
现代JavaScript允许我们使用多种技术轻松处理此问题
使用type mapFn<T, S> = (value: T, index: number, originalArray: Readonly<T[]>) => S;
interface Array<T> {
// S will not include undefined when inferred from mapFn
malter<S>(mapFn: mapFn<T, S | undefined>): S[];
}
// hey, look, this works the same way, with T|undefined => T
// instead of T => Exclude<T, undefined>
function notUndefined<T>(v: T | undefined): v is T {
return typeof v !== "undefined";
}
Array.prototype.malter = function malter<T, S>(
this: Array<T>, // inform the compiler that this is an array
mapFn: mapFn<T, S | undefined>
): S[] {
return this.reduce(function(acc: S[], val: T, index: number, orgArr: T[]) {
const el = mapFn(val, index, orgArr);
if (notUndefined(el)) {
acc.push(el);
}
return acc;
}, []);
}; // inference in implementation works
const test = [2, 3, 4, 5, 3];
// don't need any type parameters
function test1(): string[] {
return test.malter(num => (num > 3 ? num.toFixed(2) : undefined));
}
-
Array.prototype.flat
使用const arr =
[ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ]
const flatArr =
arr.flat(1) // 1 is default depth
console.log(JSON.stringify(arr))
console.log(JSON.stringify(flatArr))
// [[1],[2,3,[4,5,[6]]],[7,[8,9]]]
// [1,2,3,[4,5,[6]],7,[8,9]]
-
Array.prototype.flatMap
对const arr =
[ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ]
const flatArr =
arr.flatMap(x => x)
console.log(JSON.stringify(arr))
console.log(JSON.stringify(flatArr))
// [[1],[2,3,[4,5,[6]]],[7,[8,9]]]
// [1,2,3,[4,5,[6]],7,[8,9]]
使用传播参数
Array.prototype.concat
较旧版本的JavaScript(ECMAScript 5及更低版本)可以使用const arr =
[ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ]
const flatArr =
[].concat(...arr)
console.log(JSON.stringify(arr))
console.log(JSON.stringify(flatArr))
// [[1],[2,3,[4,5,[6]]],[7,[8,9]]]
// [1,2,3,[4,5,[6]],7,[8,9]]
-
Function.prototype.apply
使用var arr =
[ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ]
var flatArr =
Array.prototype.concat.apply([], arr)
console.log(JSON.stringify(arr))
console.log(JSON.stringify(flatArr))
// [[1],[2,3,[4,5,[6]]],[7,[8,9]]]
// [1,2,3,[4,5,[6]],7,[8,9]]
-
Array.prototype.reduce
使用原始的var arr =
[ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ]
var flatArr =
arr.reduce((r, a) => r.concat(a), [])
console.log(JSON.stringify(arr))
console.log(JSON.stringify(flatArr))
// [[1],[2,3,[4,5,[6]]],[7,[8,9]]]
// [1,2,3,[4,5,[6]],7,[8,9]]
循环-
for
答案 2 :(得分:-1)
如果您使用ES6 / ES2015,则可以使用spread运算符。像这样的东西
console.log(...[[[1],[1,2,3,[4,5],4],[2,3]]])
&#13;