Javascript对象数组中的分组总和

时间:2016-06-13 12:47:58

标签: javascript arrays javascript-objects

基于inputArray我想创建outputArray,outputArray中的标记计算如下所示

第1组=物理+化学+数学

第2组=历史+地理

第3组=语言优先+语言第二

组总数=组1 +组2 +组3

var inputArray = [
  { subject: 'Physics', marks: '60' },
  { subject: 'History', marks: '35' },
  { subject: 'Chemistry', marks: '50' }, 
  { subject: 'Language First', marks: '70' },
  { subject: 'Geography', marks: '65' },
  { subject: 'Maths', marks: '40' },
  { subject: 'Language Second', marks: '50' },
  .
  .
  .
];

var outputArray = [
  { subject: 'Group 1', marks: '150' },
  { subject: 'Group 2', marks: '100' },
  { subject: 'Group 3', marks: '120' },
  { subject: 'Group Total', marks: '370' },
  .
  .
  .
];

3 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取它:

for(var i in inputArray){
   if(inputArray[i].subject == "Physics" || inputArray[i].subject == "Chemistry" || inputArray[i].subject == "Maths")
       outputArray[0].marks+= Number(inputArray[i].marks);

   else if(inputArray[i].subject == "History" || inputArray[i].subject == "Geography")
      outputArray[1].marks+= Number(inputArray[i].marks);

  else if(inputArray[i].subject == "Language Second" || inputArray[i].subject == "Language First")
      outputArray[2].marks+= Number(inputArray[i].marks);

}

jsFiddle

希望有所帮助:)

答案 1 :(得分:0)

此提案适用于某个对象,一个作为对象组的引用,另一个作为对结果集的引用。

首先,结果集是基于所需组的构建。在此循环中,生成了引用。

其次,inputArray循环播放,marks被添加到主题所在的组中。 marks也会添加到群组总计

var inputArray = [{ subject: 'Physics', marks: '60' }, { subject: 'History', marks: '35' }, { subject: 'Chemistry', marks: '50' }, { subject: 'Language First', marks: '70' }, { subject: 'Geography', marks: '65' }, { subject: 'Maths', marks: '40' }, { subject: 'Language Second', marks: '50' }],
    groups = { 'Group 1': ['Physics', 'Chemistry', 'Maths'], 'Group 2': ['History', 'Geography'], 'Group 3': ['Language First', 'Language Second'], 'Group Total': null },
    reference = Object.create(null),
    subjects = Object.create(null),
    outputArray = Object.keys(groups).map(function (k) {
        reference[k] = { subject: k, marks: 0 };
        Array.isArray(groups[k]) && groups[k].forEach(function (a) { subjects[a] = k; });
        return reference[k];
    });

inputArray.forEach(function (a) {
    reference[subjects[a.subject]].marks += +a.marks;
    reference['Group Total'].marks += +a.marks
});

console.log(outputArray);

答案 2 :(得分:0)

使用Object.keysArray.indexOf函数和预定义groupsMap对象考虑以下解决方案:

var groupsMap = {
    'Group 1': ['Physics','Chemistry','Maths'],
    'Group 2': ['History','Geography'],
    'Group 3': ['Language First','Language Second']
}, output = {'Group Total': 0}, keys = Object.keys(groupsMap), len = keys.length;

inputArray.forEach(function(obj){
    for (var i = 0; i < len; i++) {
        if (groupsMap[keys[i]].indexOf(obj['subject']) !== -1) {
            this[keys[i]] = this[keys[i]] || 0;
            this[keys[i]] += parseInt(obj['marks']);
            this['Group Total'] += parseInt(obj['marks']);
            break;  // avoids redundant iterations
        }
    }
}, output);

var outputArray = Object.keys(output).map((k) => ({'subject': k, 'marks': output[k]}));

console.log(JSON.stringify(outputArray, 0, 4));

输出:

[
    {
        "subject": "Group Total",
        "marks": 370
    },
    {
        "subject": "Group 1",
        "marks": 150
    },
    {
        "subject": "Group 2",
        "marks": 100
    },
    {
        "subject": "Group 3",
        "marks": 120
    }
]