刷 freecodecamp 的中级 JavaScript 到此 https://freecodecamp.cn/challenges/steamroller:
而在该题目中需要 flatten
的实现:
于是手刷:
function steamroller(arrs) { ?if (!arrs || !arrs.length) throw new ReferenceError(); ?var arr = []; ?(function flatten (items) { ???items.forEach(function(item){ ?????if (item !== undefined && item !== null) { ???????if (Array.isArray(item)) { ?????????arr.push(flatten(item)); ???????} else { ?????????arr.push(item); ???????} ?????} ???}); ?}(arrs)); ?arr = arr.filter(function(item){ ???return item; ?}); ?return arr;}steamroller([1, [2], [3, [[4]]]]);