1.6 KiB
1.6 KiB
id, challengeType, forumTopicId, localeTitle
id | challengeType | forumTopicId | localeTitle |
---|---|---|---|
ab306dbdcc907c7ddfc30830 | 5 | 16079 | 扁平化 |
Description
Instructions
Tests
tests:
- text: "<code>steamrollArray([[['a']], [['b']]])</code>应该返回<code>['a', 'b']</code>。"
testString: assert.deepEqual(steamrollArray([[["a"]], [["b"]]]), ["a", "b"]);
- text: <code>steamrollArray([1, [2], [3, [[4]]]])</code>应该返回<code>[1, 2, 3, 4]</code>。
testString: assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);
- text: <code>steamrollArray([1, [], [3, [[4]]]])</code>应该返回<code>[1, 3, 4]</code>。
testString: assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
- text: <code>steamrollArray([1, {}, [3, [[4]]]])</code>应该返回<code>[1, {}, 3, 4]</code>。
testString: assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);
Challenge Seed
function steamrollArray(arr) {
// I'm a steamroller, baby
return arr;
}
steamrollArray([1, [2], [3, [[4]]]]);
Solution
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;
}