我有这个json对象
Object {casenum: "2015-7-001", month: "7", casenature: "1", resolution: "Mediation"}
Object {casenum: "2015-7-001", month: "7", casenature: "1", resolution: "Conciliation"}
我计算每个月的分辨率数量,并计算个案数量。在这个例子中,因为casenum是相同的,这意味着只有这样的情况我只需要添加一个大小写,但因为它们在数组中出现两次,所以它与casenature的计数相同两倍。我只想算他一次,因为它是同一个案例。我像这样计算分辨率
var Monthcounter = 0;
var Civilcounter = 0;
var Criminalcounter = 0;
var Mediationcounter = 0;
var Conciliationcounter = 0;
var Arbitrationcounter = 0;
for (var obj in data) {
console.log(data[obj]);
var month = data[obj];
var casenature = data[obj];
if (month.hasOwnProperty('month')) {
Monthcounter++;
if (month.resolution == "Mediation") {
Mediationcounter++;
}
if (month.resolution == "Conciliation") {
Conciliationcounter++;
}
if (month.resolution == "Arbitration") {
Arbitrationcounter++;
}
if (casenature.hasOwnProperty('casenature')) {
if (casenature.casenature == "1") {
Civilcounter++;
}
if (casenature.casenature == "0") {
Criminalcounter++;
}
}
}
}
UPDATE
var oneArray = [];
//Your code
if (casenature.hasOwnProperty('casenum')) { //Your bool check
oneArray.push({
casenum: casenature.casenum
}); //Add obj to the array
console.log(oneArray);
var hasNum = false;
$.each(oneArray, function(i, objOne) { //Each iterates through object in oneArray
console.log(objOne.casenum);
console.log(casenature.casenum);
if (objOne.casenum == casenature.casenum) { //Check casenum unique in dataset 'data'
console.log("");
hasNum = true;
return false; //Break iteration, need only 1 negative to break loop
}
});
if (!hasNum) { //Only add counter if casenum is unique in dataset
if (casenature.casenature == "1") {
Civilcounter++;
}
if (casenature.casenature == "0") {
Criminalcounter++;
}
}
}
答案 0 :(得分:0)
您可以创建两个要在此范围内使用的数组,以检查您是否已经通过具有casenum的对象添加了计数器。
根据您是否只想将计数器添加到唯一的casenums,或者casenum AND casenature必须是唯一的,您将构建嵌套的if(){}
逻辑。
for (var obj in data) {
var zeroArray = [];
var oneArray = [];
//Your code
if (casenature.casenature == "1") { //Your bool check
oneArray.push(obj); //Add obj to the array
var hasNum = false;
$.each(oneArray, function(i,objOne) { //Each iterates through object in oneArray
if (objOne.casenum == obj.casenum) //Check casenum unique in dataset 'data'
{
hasNum = true;
return false; //Break iteration, need only 1 negative to break loop
}
});
if (!hasNum) //Only add counter if casenum is unique in dataset
{
Civilcounter++;
}
}
}
}