javascript - Find array count of fields using lodash or underscorejs -


i have requirement count of each account type in array. have tried normal way of looping array , increment counter each account_type. please me same in lodash or underscore js.

your appreciated. thank you.

input_array = [     {         account_id: '4304bf0381140b8fcccbdddea3571a53facebook',         account_type: 'facebook'     },     {         account_id: '4304bf0381140b8fcccbdddea332434facebook',         account_type: 'facebook'     },     {         account_id: '5824fb40c4a97e21ef9715ea69c1cfb9twitter',         account_type: 'twitter'     },     {         account_id: '5824fb40c4a97e21ef9715ea432423twitter',         account_type: 'twitter'     },     {         account_id: '2c13790be20a06a35da629c71e548afexing',         account_type: 'xing'     },     {         account_id: 'd1376b07b144130c4f041e223dfb1197weibo',         account_type: 'weibo'     },     {         account_id: 'd1376b07b144130c4f041e223df4343weibo',         account_type: 'weibo'     } ] output_array = [     {         type: "facebook",         count: 2     },     {         type: "twitter",         count: 2     },     {         type: "xing",         count: 1     },     {         type: "weibo",         count: 2     } ] 

tried

input_array.foreach(function(data){     if(data.account_type=='facebook'){         count++;//forfacebook     } }); 

you can use countby...

_.countby(input_array, function(obj) {     return obj.account_type; }); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -