1.3 KiB
1.3 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| ab306dbdcc907c7ddfc30830 | 数组扁平化 | 5 | 16079 | steamroller |
--description--
嵌套数组扁平化成一维数组。 必须考虑到各种深度的嵌套层级。
--hints--
steamrollArray([[["a"]], [["b"]]]) 应返回 ["a", "b"]。
assert.deepEqual(steamrollArray([[['a']], [['b']]]), ['a', 'b']);
steamrollArray([1, [2], [3, [[4]]]]) 应返回 [1, 2, 3, 4]。
assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);
steamrollArray([1, [], [3, [[4]]]]) 应返回 [1, 3, 4]。
assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
steamrollArray([1, {}, [3, [[4]]]]) 应返回 [1, {}, 3, 4]。
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);
代码中不应使用 Array.prototype.flat() 或 Array.prototype.flatMap() 方法。
assert(!code.match(/\.\s*flat\s*\(/) && !code.match(/\.\s*flatMap\s*\(/));
--seed--
--seed-contents--
function steamrollArray(arr) {
return arr;
}
steamrollArray([1, [2], [3, [[4]]]]);
--solutions--
function steamrollArray(arr) {
if (!Array.isArray(arr)) {
return [arr];
}
var out = [];
arr.forEach(function(e) {
steamrollArray(e).forEach(function(v) {
out.push(v);
});
});
return out;
}