Lodash has a nice chunk method for arrays; is there an equivalent for objects (associative arrays)? The imperative solution is pretty straight forward, but I'm just curious if there is a cleaner functional implementation?
Imperative solution:
Takes an object as input
Returns an array of objects with size
number of properties
var chunk = function(input, size){
var obj = {};
var output = [];
var counter = 0;
for (var k in input) {
if(!input.hasOwnProperty(k)){
continue;
}
obj[k] = input[k];
if (++counter % size === 0) {
output.push(obj);
obj = {};
}
}
if (Object.keys(obj).length !== 0){
output.push(obj);
}
return output;
};
_.pairs
, then chunk, and_.object
at the end?