我有一个要求,获取数组中每个帐户类型的计数。我已经尝试过循环数组并为每个account_type增加一个计数器的正常方法。你能不能帮我做同样的事在寄宿或下划线js。
非常感谢你的帮助。谢谢。
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
}
]试过
input_array.forEach(function(data){
if(data.account_type=='facebook'){
count++;//forfacebook
}
});发布于 2015-06-19 04:42:39
你可以用countBy..。
_.countBy(input_array, function(obj) {
return obj.account_type;
});发布于 2015-06-19 04:44:34
您可以使用filter()
var arrLen = _.filter(input_array, function(obj){
console.log(obj)
return obj.account_type === "facebook"
}).length;https://stackoverflow.com/questions/30929983
复制相似问题