从jsonObject计数数据

时间:2018-11-01 11:01:35

标签: javascript json

我有这个json对象数组

 [{"Subject":"Maths","status:"Pass"},{"Subject":"Maths","status:"Pass"},
     {"Subject":"Maths","status:"Fail"},{"Subject":"Maths","status:"Fail"},
    {"Subject":"English","status:"Pass"},{"Subject":"English","status:"Fail"}]

我想计算每个主题的及格和不及格的数量,并将它们存储在对象数组中,如

[{"Subject":"Maths","Pass":2,"Fail":2},"Subject":"English","Pass":2,"Fail":2}]

此数据来自api,主题也可能有所不同,可能有很多主题。

3 个答案:

答案 0 :(得分:1)

使用reduce函数,并在累加器数组中检查Subject是否存在。如果不存在,则创建一个新对象并设置键SubjectPassFail的值。 如果存在,则更新“通过和失败”的值

let k = [{
    "Subject": "Maths",
    "status": "Pass"
  },
  {
    "Subject": "Maths",
    "status": "Pass"
  },
  {
    "Subject": "Maths",
    "status": "Fail"
  },
  {
    "Subject": "Maths",
    "status": "Fail"
  },
  {
    "Subject": "English",
    "status": "Fail"
  },
  {
    "Subject": "English",
    "status": "Pass"
  }
]

let res = k.reduce((acc, curr) => {
  let findSubjectIndex = acc.findIndex((item) => {
    return item.Subject === curr.Subject;
  })
  if (findSubjectIndex === -1) {
    let crtOb = Object.assign({}, {
      Subject: curr.Subject,
      Pass: curr.status === "Pass" ? 1 : 0,
      Fail: curr.status === "Fail" ? 1 : 0
    })
    acc.push(crtOb)

  } else {
    console.table(acc)
    acc[findSubjectIndex].Pass = acc[findSubjectIndex].Pass + (curr.status === "Pass" ? 1 : 0);
    acc[findSubjectIndex].Fail = acc[findSubjectIndex].Fail + (curr.status === "Fail" ? 1 : 0);
  }
  return acc;
}, []);
console.log(res)

答案 1 :(得分:1)

使用Array.reduceObject.values

let arr = [{"Subject":"Maths","status":"Pass"},{"Subject":"Maths","status":"Pass"},
     {"Subject":"Maths","status":"Fail"},{"Subject":"Maths","status":"Fail"},
    {"Subject":"English","status":"Pass"},{"Subject":"English","status":"Fail"}];

// Create an obect with key as subject and value as desired object.
let result = Object.values(arr.reduce((a, {Subject, status}) => {
  // if subject exists in object, updating object with count
  if(a[Subject]) a[Subject][status] = (a[Subject][status] || 0) + 1;
  // else adding a new entry for subject
  else a[Subject] = {Subject, [status] : 1};
  return a;
}, {}));
console.log(result);

答案 2 :(得分:0)

像我这样的新手更易读易懂的代码

var jsonObject  = [{"Subject":"Maths","status":"Pass"},{"Subject":"Maths","status":"Pass"},
     {"Subject":"Maths","status":"Fail"},{"Subject":"Maths","status":"Fail"},
    {"Subject":"English","status":"Pass"},{"Subject":"English","status":"Fail"}];

var passMathCount = 0;
var failMathCount = 0;
var passEnglishCount= 0;
var failEnglishCount = 0;

//Iterate through each object
for(var i = 0; i < jsonObject.length; i++) {
    
    var obj = jsonObject[i];
    //Check combination for respective requirements
    if(obj.status == "Pass" && obj.Subject == "Maths")
      passMathCount++;
    else if(obj.status == "Fail" && obj.Subject == "Maths")
      failMathCount++;
    else if(obj.status == "Pass" && obj.Subject == "English")
      passEnglishCount++;
    else
      failEnglishCount++;
}

//Build json object array
var jsonInstance = [{"Subject": "Maths", "pass": passMathCount, "fail":failMathCount},
                    {"Subject": "English", "pass": passEnglishCount, "fail":failEnglishCount}];
                    
console.log(jsonInstance);