Underscore.js 中文文档
Functionally Similar - Comparing Underscore.js to LINQ
holmes2136
group by sample
var list = [ { productId: 1, unitId: 19, newId: 33 }, { productId: 1, unitId: 20, newId: 33 }, { productId: 1, unitId: 20, newId: 13 }, { productId: 1, unitId: 20, newId: 70 }, { productId: 1, unitId: 21, newId: 13 }, { productId: 1, unitId: 21, newId: 20 } ]; var groups = _.groupBy(list, function(value){ return value.productId + '#' + value.unitId; }); console.dir(groups); var data = _.map(groups, function(group){ //console.dir(group.length); return { productId: group[0].productId, unitId: group[0].unitId, newIds: _.pluck(group, 'newId') }; }); var groups2 = _.groupBy(list, function(obj){ return JSON.stringify({productId:obj.productId, unitId:obj.unitId}); }); console.dir(groups2); var matchs = _.reject(groups2, function(group){ return group.length === 1; }); console.dir(matchs);
沒有留言:
張貼留言