Sunday, 12 March 2017

lodash split array into chunk in nodejs

I have below group with contacts

 var groups = {"GROUP":[{'G1: [C1,C2,C3........C500000]},
             {'G2': [C1,C2,C3........C500000,D1,D2,D3........D500000]}
.....]
    }

Now i am split a javascript array into n sized chunks using lodash as like below

_.each(groups,function(group){
 var n = 50;
var lists = _.groupBy(group, function(element, index){
  return Math.floor(index/n);
});
lists = _.toArray(lists );
console.log(lists )
})

This works fine in client end but not in nodejs. Output from node js as below

[["C1","C1....]]

Array is not split in to chunks instead it is coming as single array.

Expected array should be

[["C1","C1"....,"c50"],["C51","C1"....,"c100"]...]

Please advise



via mymotherland

No comments:

Post a Comment