如何转换为:
[ {food: 'apple', type: 'fruit'}, {food: 'potato', type: 'vegetable'}, {food: 'banana', type: 'fruit'}, ]
到这个:
[ {type: 'fruit', foods: ['apple', 'banana']}, {type: 'vegetable', foods: ['potato']} ]
使用javascript或下划线
假设原始列表包含在名为的变量中list:
list
_ .chain(list) .groupBy('type') .map(function(value, key) { return { type: key, foods: _.pluck(value, 'food') } }) .value();