循环遍历一组对象并获得平均值

时间:2013-12-20 14:10:53

标签: javascript

我希望在我的原型函数中循环遍历一个对象数组。在我循环之后,我想获得gpa数组的平均值,然后控制台记录名称,地址,gpa和平均gpa。除了平均水平,我能做到。我希望这一切都在测试功能中一起运行。感谢您的帮助。

var students = [{
    name: "Walker",
    address: {
        street: "123 South Drive",
        city: 'Sarasota',
        state: 'FL'
    },
    gpa: [3.0, 3.4, 3.8]
}, {
    name: "Christian",
    address: {
        street: "5601 Pebble Beach Ln",
        city: 'Sacromento',
        state: 'CA'
    },
    gpa: [2.5, 3.6, 3.8]
}];

var stud = Students(students);



stud.testing();

constuctor.js:

var Students = function (students) {
    return new Students.prototype.init(students);
}

Students.prototype = {
    init: function (students) {
        this.students = students;
    },
    testing: function () {


        for (i = 0; i < this.students.length; i++) {
            i = i % this.students.length;


            for (j = 0; j < this.students.length; j++) {



                this.gpa1 = this.students[j].gpa;
                this.len = this.students[j].gpa.length;

                //console.log(this.gpa1);

                this.average = this.gpa1[0] + this.gpa1[1] + this.gpa1[2];
                this.res = this.average / this.len;
                console.log(this.res);
            }




            console.log("Name: " + this.students[i].name);
            console.log("Address: " + this.students[i].address.street + ' ' + this.students[i].address.city + ' ' + this.students[i].address.state);
            console.log("GPA: " + this.students[i].gpa);
            console.log("Average: " + this.gpa1);


        }
    },
    addData: function (student3) {
        this.students.push(student3);
    }

3 个答案:

答案 0 :(得分:1)

你有一个起点来获得平均值,然而,看起来你将它作为&#34;总计&#34;代替:

this.average = this.gpa1[0] + this.gpa1[1] + this.gpa1[2];

现在,假设只有三个等级,我们可以做一个小改动:

this.average = (this.gpa1[0] + this.gpa1[1] + this.gpa1[2]) / 3.0;

如果你想要一个更动态的平均值,虽然可能不会出现这种情况,你可以循环执行计算:

for (var avg = 0; avg < this.gpa1.length; avg++) {
    this.average += this.gpa1[avg];
}
this.average = this.average / this.gpa1.length;

我上面的两个解决方案都是为了计算每个学生的平均成绩;如果您需要计算所有学生的总平均值,那么对上述循环的调整应该足够了,但是对于更精确的示例,请查看@MattBurland's answer

答案 1 :(得分:1)

这应该很简单,循环学生,然后循环他们的GPA分数,加起来并除以总分数:

testing: function () {
    var total = 0;
    var numPoints = 0;
    for (var i = 0; i < this.students.length; i++) {
        var thisStudentsTotal = 0;
        for (var j = 0; j < this.students[i].gpa.length; j++) {
            total += this.students[i].gpa[j];
            thisStudentsTotal += this.students[i].gpa[j];
            numPoints++;
        }
        // This is the average for just this student
        this.students[i].myAvg = thisStudentsTotal / this.students[i].gpa.length;
    }
    // This is the average for every student
    var theAverage = total / numPoints;
}

答案 2 :(得分:0)

我倾向于在循环之前将GPA平均值添加到对象中。这样,您可以在程序的其他部分重用它,而无需重复代码。

在这里,我使用了Array.mapArray.reduce方法为每个学生对象添加平均值(小数点后两位),因为我觉得它更整洁。

this.students.map(function (el) {
  el.avg = (el.gpa.reduce(function (p, c) {
    return p + c;
  }) / el.gpa.length).toFixed(2);
  return el;
});

现在你只是遍历对象。

for (var i = 0, l = this.students.length; i < l; i++) {
  var student = this.students[i];
  console.log("Name: " + student.name);
  // etc
}

I've added a fiddle其中包含您的代码,在我看到错误的地方略微纠正。我将getAverageGpa代码分成了一个新函数。