
[英]Sum the json array based on the occurance of the string in the key and store it in the separate json array with the string and its count
[英]Count Repeated Values in Json String and store value and label in another String
我需要计算json数组中重复项的数量,并将重复项显示为单个项,并应显示重复了多少条领带。
请检查以下代码和输出,以便您了解:
[{
"Name": "Jacob",
"Gender": "Male",
"City": "Newyork",
"State": "Newyork"
}, {
"Name": "Mason",
"Gender": "Male",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Ethan",
"Gender": "Male",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Noah",
"Gender": "Male",
"City": "Newyork",
"State": "NewYork"
}, {
"Name": "Sophia",
"Gender": "Female",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Emma",
"Gender": "Female",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Isabella",
"Gender": "Female",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Olivia",
"Gender": "Female",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Elizabeth",
"Gender": "Female",
"City": "Newyork",
"State": "Newyork"
}, {
"Name": "zoey",
"Gender": "Female",
"City": "Newyork",
"State": "Newyork"
}]
所需的json输出是
Newyork : 4Times,
芝加哥:3次,
请找到小提琴:
http://jsfiddle.net/vamsikrishna981/dMrNv/
试试这个http://jsfiddle.net/RHYFC/
参见小提琴的长版本和短版本
使用Javascript
var result = {};
for(var i = 0; i < data.length; i++){
for(var item in data[i]){
if(!result[item]){
result[item] = {};
}
if(!result[item][data[i][item]]){
result[item][data[i][item]] = 0;
}
result[item][data[i][item]]++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.