Pregunta

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

¿Fue útil?

Solución

You can use _.flatten function with shallow argument True, like this

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top