2016-07-27 82 views
-2

我有一个JSON对象从AJAX请求返回给我。我知道如何合计一个数组/对象中属于某个键的值的总和。但我怎么能动态地做到这一点?这可能很简单,我只是错过了一点,我可以想象它涉及另一个数组。 我使用的示例是从每个国家/地区获得总数,我从该示例中删除了不相关的信息。如何获取数组中唯一值的数量?

{ 
"message": [ 
     { 
     "login_country_name": "Great Britain" 
     }, 
     { 
     "login_country_name": "Great Britain" 
     }, 
     { 
     "login_country_name": "United States" 
     }, 
     { 
     "login_country_name": "Localhost" 
     }, 
     { 
     "login_country_name": "Netherlands" 
     } 
    ] 
} 

期待的结果是类似的东西:

("Great Britain": 2, "United States": 1, "Localhost": 1, "Netherlands": 1) 
+0

总的是什么?发生次数? –

+0

你的问题不清楚。我从上下文中假设你试图获得唯一值的数量? –

+2

好像你也删除了相关信息;) –

回答

3

你只需要遍历数据,并为每一个独特的价值计数。

var d = { 
 
    "message": [{ 
 
    "login_country_name": "Great Britain" 
 
    }, { 
 
    "login_country_name": "Great Britain" 
 
    }, { 
 
    "login_country_name": "United States" 
 
    }, { 
 
    "login_country_name": "Localhost" 
 
    }, { 
 
    "login_country_name": "Netherlands" 
 
    }] 
 
} 
 
var r = {}; 
 

 
d.message.forEach(function(o){ 
 
    r[o.login_country_name] = (r[o.login_country_name] || 0) +1 
 
}); 
 

 
console.log(r)

+0

我喜欢这是多么简单,很好地完成! – LukeXF

4

迭代阵列之上并生成结果。你可以使用Array#reduce方法。

var data = { 
 
    "message": [{ 
 
    "login_country_name": "Great Britain" 
 
    }, { 
 
    "login_country_name": "Great Britain" 
 
    }, { 
 
    "login_country_name": "United States" 
 
    }, { 
 
    "login_country_name": "Localhost" 
 
    }, { 
 
    "login_country_name": "Netherlands" 
 
    }] 
 
}; 
 

 
var res = data.message.reduce(function(obj, v) { 
 
    // update country count , where `(obj[v.login_country_name] || 0)` helps to avoid undefined retrns `0` if undefined 
 
    obj[v.login_country_name] = (obj[v.login_country_name] || 0) + 1; 
 
    // return the updated object 
 
    return obj; 
 
    // set initial value as an object which stores the result 
 
}, {}) 
 

 
console.log(res);

2

你可以使用一个对象和国家的钥匙。

var object = { "message": [{ "login_country_name": "Great Britain" }, { "login_country_name": "Great Britain" }, { "login_country_name": "United States" }, { "login_country_name": "Localhost" }, { "login_country_name": "Netherlands" }] }, 
 
    count = Object.create(null); 
 

 
object.message.forEach(function (a) { 
 
    count[a.login_country_name] = (count[a.login_country_name] || 0) + 1; 
 
}); 
 

 
console.log(count);

0
var obj = { 
    "message": [{ 
    "login_country_name": "Great Britain" 
    }, { 
    "login_country_name": "Great Britain" 
    }, { 
    "login_country_name": "United States" 
    }, { 
    "login_country_name": "Localhost" 
    }, { 
    "login_country_name": "Netherlands" 
    }] 
} 
var _getObj = obj.message; 
var grouped=[]; 
_getObj.forEach(function(item) { 
    if (!this[item["login_country_name"]]) { // if item dont exist then create a new 
    this[item["login_country_name"]] = { 

     name: item["login_country_name"], 
     qty: 0 
     }; 
     grouped.push(this[item["login_country_name"]]) 
    } 
    this[item["login_country_name"]].qty = this[item["login_country_name"]].qty + 1; 


}, Object.create(null)) // create an empty object 
console.log(grouped) 

JSFIDDLE

0

这是你需要什么?

var message = [ 
 
    { 
 
    "login_country_name": "Great Britain" 
 
    }, 
 
    { 
 
    "login_country_name": "Great Britain" 
 
    }, 
 
    { 
 
    "login_country_name": "United States" 
 
    }, 
 
    { 
 
    "login_country_name": "Localhost" 
 
    }, 
 
    { 
 
    "login_country_name": "Netherlands" 
 
    }, 
 
    { 
 
    "login_country_name": "Great Britain" 
 
    } 
 
]; 
 

 
var newMessage = []; 
 
$.each(message, function(key, value) { 
 
    if (newMessage.length < 1) { 
 
    newMessage.push(value); 
 
    
 
    } else { 
 
    var dIndex = -1; 
 
    $.each(newMessage, function(idx, val) { 
 
     if (value.login_country_name === val.login_country_name) dIndex = idx; 
 
    }); 
 
    
 
    if (dIndex == -1) { 
 
     value.count = 1; 
 
     newMessage.push(value); 
 
    } else { 
 
     newMessage[dIndex].count = (newMessage[dIndex].count || 1) + 1; 
 
    }  
 
    } 
 
}); 
 

 
console.log(newMessage);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>