简体   繁体   English

CoffeeScript:使用lodash(_ .js)将列表合并到单个列表中

[英]CoffeeScript: Merge List of List to a single List using lodash(_ .js)

I have an array of lists, I want to merge that into a single list. 我有一个列表数组,我想将它合并到一个列表中。

merged = []
_.map lists , (each_list) ->
    merged = _.merge each_list , merged

What am i doing wrong here, I am only getting one list out of all the lists in the aggregated output. 我在这里做错了什么,我只从汇总输出中的所有列表中得到一个列表。

FYI, below block is called inside a final_callback of async.map 在块的下面是在async.map的async.map

You can use _.flatten function with shallow argument True , like this 你可以使用带有shallow参数True _.flatten函数,就像这样

var arrays = [[1, [2], 3], [4, [5], 6], [7, [8], 9]];
console.log(_.flatten(arrays, true));
# [ 1, [ 2 ], 3, 4, [ 5 ], 6, 7, [ 8 ], 9 ]

If we don't flatten the arrays in shallow mode, it will recursively flatten all the nested arrays, like this 如果我们不以浅模式展平数组,它会递归地展平所有嵌套数组,就像这样

console.log(_.flatten(arrays));
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Lodash also has a similar function, with the same name, _.flatten . Lodash也有类似的功能,同名, _.flatten . _.flatten

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM