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

IT技术 javascript arrays javascript-objects
2021-01-15 10:33:05

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

输入:

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个回答

简单的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);

您可以使用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);


仅供参考:您可以key通过使用find()方法来避免该对象,但在性能方面可能会慢一点。

当 map 和 reduce 用更少的代码做同样的事情时,为什么要使用 for each 呢?
2021-04-05 10:33:05
@evolutionxbox :我认为 foreach 更适合这个
2021-04-06 10:33:05
@evolutionxbox : map 不适合这个......因为结果计数较少......使用reduce可以做到,甚至有相同的行
2021-04-09 10:33:05
你能解释一下为什么吗?(不是无礼,只是好奇)
2021-04-13 10:33:05

你可以做到这一点forEach,并thisArg可选参数

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)

谢谢 Nenad,这是想要的。我在这里还想要一件事,我有一个更完全相似的数组,如 inputArray。那就是factorArray。计算就像 outputArrayObject.marks = factorArrayObject.marks - (您当前所做的标记总和)
2021-04-02 10:33:05
factorArray 对象顺序与 outputArray 不同,并且与 outputArray 相比,factorArray 计数中的对象或多或少
2021-04-05 10:33:05
那么你可以做到这一点,但它会工作,如果在你的factorArray对象是相同的顺序,在outputArray对象jsfiddle.net/Lg0wyt9u/913其他你需要对它们进行排序。
2021-04-10 10:33:05

使用Array.forEach,parseIntObject.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
    }
]

干得好:

function convert(inputArray) {
    var temp = {};
    for(var i = 0; i < inputArray.length; i++) {
        var subject = inputArray[i].subject;

        // check if there is already an entry with this subject in the temp array
        if(temp.hasOwnProperty(subject)) {
            // if it is already in the list, add the marks and the noOfStudents
            temp[subject].marks = temp[subject].marks + parseInt(inputArray[i].marks, 10);
            temp[subject].noOfStudents = temp[subject].noOfStudents + parseInt(inputArray[i].noOfStudents, 10);
        }
        else {
            // if it is not yet in the list, add a new object
            temp[subject] = {
                subject: subject,
                marks: parseInt(inputArray[i].marks, 10),
                noOfStudents: parseInt(inputArray[i].noOfStudents, 10)
            }
        }
    }

    // the temporary array is based on the subject, you are however interested on the effective value object
    var result = [];
    for(var entryKey in temp) {
        result.push(temp[entryKey]);
    }

    return result;
}