underscore.js - Group objects by property in javascript -


how convert this:

[     {food: 'apple', type: 'fruit'},     {food: 'potato', type: 'vegetable'},     {food: 'banana', type: 'fruit'}, ] 

into this:

[     {type: 'fruit', foods: ['apple', 'banana']},     {type: 'vegetable', foods: ['potato']} ] 

using javascript or underscore

assuming original list contained in variable named list:

_ .chain(list) .groupby('type') .map(function(value, key) {     return {         type: key,         foods: _.pluck(value, 'food')     } }) .value(); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -