Javascript中新对象数组中数组对象属性值的总和

时间:2016-05-27 10:50:01

标签: javascript arrays javascript-objects

我有一个对象数组,我需要在新的对象数组中对象属性值的总和,

输入:

var inputArray = [
  { subject: 'Maths', marks: '40', noOfStudents: '5' },
  { subject: 'Science', marks: '50', noOfStudents: '16' },
  { subject: 'History', marks: '35', noOfStudents: '23' },
  { subject: 'Science', marks: '65', noOfStudents: '2' },
  { subject: 'Maths', marks: '30', noOfStudents: '12' },
  { subject: 'History', marks: '55', noOfStudents: '20' },
  .
  .
  .
];

我需要的输出,

var outputArray = [
  { subject: 'Maths', marks: '70', noOfStudents: '17' },
  { subject: 'Science', marks: '115', noOfStudents: '18' },
  { subject: 'History', marks: '95', noOfStudents: '43' },
  .
  .
  .
];

我想要新对象数组中的标记总数和主题学生数。输入数组中会有N个其他主题对象(即地理,物理等)。

6 个答案:

答案 0 :(得分:2)

简单的reduce解决方案:

const data = [
  { subject: 'Maths', marks: '40', noOfStudents: '5' },
  { subject: 'Science', marks: '50', noOfStudents: '16' },
  { subject: 'History', marks: '35', noOfStudents: '23' },
  { subject: 'Science', marks: '65', noOfStudents: '2' },
  { subject: 'Maths', marks: '30', noOfStudents: '12' },
  { subject: 'History', marks: '55', noOfStudents: '20' }];

const result = data.reduce((cur, val) => {
  let alreadyIn = cur.find(e => e['subject'] == val['subject']);
  if (alreadyIn) {
    alreadyIn['marks'] = (parseInt(alreadyIn['marks']) + parseInt(val['marks'])).toString();
    alreadyIn['noOfStudents'] = (parseInt(alreadyIn['noOfStudents']) + parseInt(val['noOfStudents'])).toString();
  } else {
    cur.push(val);
  }
  return cur;
}, []);

console.log(result);

答案 1 :(得分:1)

您可以使用 forEach() 来迭代并生成新数组

var inputArray = [
  { subject: 'Maths', marks: '40', noOfStudents: '5' },
  { subject: 'Science', marks: '50', noOfStudents: '16' },
  { subject: 'History', marks: '35', noOfStudents: '23' },
  { subject: 'Science', marks: '65', noOfStudents: '2' },
  { subject: 'Maths', marks: '30', noOfStudents: '12' },
  { subject: 'History', marks: '55', noOfStudents: '20' }
],
    res = [],
    key = {};

inputArray.forEach(function(v) {
  if (key.hasOwnProperty(v.subject)) { // check subject already added by using key object
    res[key[v.subject]].marks += Number(v.marks); //incase already exist parse number and add
    res[key[v.subject]].noOfStudents += Number(v.noOfStudents);
  } else {
    key[v.subject] = res.length; // create index entry in key object
    res.push({ // push the value 
      'subject': v.subject,
      'marks': Number(v.marks),
      'noOfStudents': Number(v.noOfStudents)
    })
    // if you pushed the original object then the original array also will get updated while adding the mark, so never push the refernce
  }
})

console.log(res);

使用 reduce() 方法

var inputArray = [
  { subject: 'Maths', marks: '40', noOfStudents: '5' },
  { subject: 'Science', marks: '50', noOfStudents: '16' },
  { subject: 'History', marks: '35', noOfStudents: '23' },
  { subject: 'Science', marks: '65', noOfStudents: '2' },
  { subject: 'Maths', marks: '30', noOfStudents: '12' },
  { subject: 'History', marks: '55', noOfStudents: '20' }
],
    key = {};

res=inputArray.reduce(function(arr,v) {
  if (key.hasOwnProperty(v.subject)) { // check subject already added by using key object
    arr[key[v.subject]].marks += Number(v.marks); //incase already exist parse number and add
    arr[key[v.subject]].noOfStudents += Number(v.noOfStudents);
  } else {
    key[v.subject] = arr.length; // create index entry in key object
    arr.push({ // push the value 
      'subject': v.subject,
      'marks': Number(v.marks),
      'noOfStudents': Number(v.noOfStudents)
    })
    // if you pushed the original object then the original array also will get updated while adding the mark, so never push the refernce     
  }
  return arr;
},[])

console.log(res);

仅供参考:您可以使用 find() 方法来避开key对象,但性能方面可能稍微慢一些。

答案 2 :(得分:1)

您可以使用forEachthisArg可选参数

执行此操作



var inputArray = [
  { subject: 'Maths', marks: '40', noOfStudents: '5' },
  { subject: 'Science', marks: '50', noOfStudents: '16' },
  { subject: 'History', marks: '35', noOfStudents: '23' },
  { subject: 'Science', marks: '65', noOfStudents: '2' },
  { subject: 'Maths', marks: '30', noOfStudents: '12' },
  { subject: 'History', marks: '55', noOfStudents: '20' },
], outputArray = [];
  
inputArray.forEach(function(e) {
  if(!this[e.subject]) {
    this[e.subject] = { subject: e.subject, marks:  0, noOfStudents: 0 }
     outputArray.push(this[e.subject]);
   }
  this[e.subject].marks += Number(e.marks);
  this[e.subject].noOfStudents += Number(e.noOfStudents);
}, {});

console.log(outputArray)




答案 3 :(得分:0)

你走了:

ORIGINAL_SYNC_ID

答案 4 :(得分:0)

使用Array.forEachparseIntObject.keys函数的解决方案:

var summed = {}, result;
inputArray.forEach(function (obj) {
    obj['marks'] = parseInt(obj['marks']);
    obj['noOfStudents'] = parseInt(obj['noOfStudents']);
    var subj = obj['subject'];

    if (!summed[subj]) {
        summed[subj] = obj;
    } else {
        summed[subj]['marks'] += obj['marks'];
        summed[subj]['noOfStudents'] += obj['noOfStudents'];
    }
}, summed);
result = Object.keys(summed).map((k) => summed[k]);

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

输出:

[
    {
        "subject": "Maths",
        "marks": 70,
        "noOfStudents": 17
    },
    {
        "subject": "Science",
        "marks": 115,
        "noOfStudents": 18
    },
    {
        "subject": "History",
        "marks": 90,
        "noOfStudents": 43
    }
]

答案 5 :(得分:0)

使用对象作为哈希表的另一个提案。

var inputArray = [{ subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', marks: '65', noOfStudents: '2' }, { subject: 'Maths', marks: '30', noOfStudents: '12' }, { subject: 'History', marks: '55', noOfStudents: '20' }],
    outputArray = [];
inputArray.forEach(function (a) {
    if (!this[a.subject]) {
        this[a.subject] = { subject: 'Maths', marks: '0', noOfStudents: '0' };
        outputArray.push(this[a.subject]);
    }
    this[a.subject].marks = (+this[a.subject].marks + +a.marks).toString();
    this[a.subject].noOfStudents = (+this[a.subject].noOfStudents + +a.noOfStudents).toString();
}, Object.create(null));

console.log(outputArray);