Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level
将多维嵌套数组转换成一维数组,如果shallow的值为true,数组嵌套级别只提升一级
1 _.flatten([1, [2], [3, [[4]]]]); 2 => [1, 2, 3, 4]; 3 4 _.flatten([1, [2], [3, [[4]]]], true); 5 => [1, 2, 3, [[4]]]
源码:
1 _.flatten = function(array, shallow) { 2 return _.reduce(array, function(memo, value) { 3 if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value)); 4 memo[memo.length] = value; 5 return memo; 6 },