-5

I have the following array

  DataArray = [{
    "Year": "Year 7",
        "test1": 28,
        "test2": 19,
         "test3": 22,
         "average": ''

}, {
    "Year": "Year 8",
        "test1": 15,
        "test2": 28,
        "test3": 22,
        "average": ''           

}, {
    "Year": "Year 9",
        "test1": 25,
        "test2": 24,
         "test3": 22,
        "average": ''
 }, {
    "Year": "Year 10",
        "test1": 26,
        "test2": 19,
         "test3": 22,
        "average": ''            
}, {
    "Year": "Year 11",
        "test1": 48,
        "test2": 52,
         "test3": 22,
        "average": ''            
}];

How can I make it so that the value of 'average' is the average of test 1, test 2, and test 3 in each object?

craig
  • 571
  • 2
  • 4
  • 13
  • 2
    Erm ... Calculate it?! Or did I get it wrong? – Haudegen Mar 18 '14 at 17:32
  • you can't calculate it within the declaration since you can't reference the other properties within the declaration. You'll need to loop through it afterwards and do some math to calculate, e.g. `Math.round((DataArray[x].test2+DataArray[x].test3+DataArray[x].test1)/3)` where `x` is the current iteration of the loop. Note: it would probably be better if you put combined your `testN` properties into a single array – CrayonViolent Mar 18 '14 at 17:33
  • 2
    What's the actual problem here? You don't know how to loop? To add? To divide? – cookie monster Mar 18 '14 at 17:37

4 Answers4

6
dataArray.forEach(function(x) { x.average = (x.test1 + x.test2 + x.test3)/3 });

Will only work on browsers that support forEach which should be most modern browsers.

Also note that dataArray should be camelCase it is a highly recommended naming convention in javascript for everything to start with a lower case except for functions that should be invoked with new.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • 1
    There's a polyfill at the bottom of [this page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) if it's needed. – Andy Mar 18 '14 at 17:36
  • Deleted comment asked: "Where does forEach come from?" Answer is: It's a member of Array.prototype and is supported on every browser > IE8. If you need earlier support it could be a good idea to use polyfills like those from sugar.js, or libraries like underscore, or lodash – George Mauer Mar 18 '14 at 17:36
  • I guess I should correct myself that sugar.js isn't technically for polyfills. but does employ very similar techniques. – George Mauer Mar 18 '14 at 17:43
3

If you need to support earlier than IE 9 you could do it like this:

for (var i = 0, data; i < DataArray.length; i ++) {
    data = DataArray[i];
    data.average = (data.test1 + data.test2 + data.test3) / 3;
}
net.uk.sweet
  • 12,444
  • 2
  • 24
  • 42
1

You can achieve it with a simple loop:

var avg = 0;
for(i in DataArray) {
    avg = DataArray[i]["test1"] + DataArray[i]["test2"] + DataArray[i]["test3"];
    DataArray[i]["average"] = avg / 3;
    alert(DataArray[i]["average"]);
}
1

This is the basic idea, as everybody else noted. I've used jQuery to output the values to screen. Otherwise, it is not needed in this solution.

dataArray.forEach(function (e, i) {

    var average = Math.floor((e.test1 + e.test2 + e.test3) / 3);

    $('<span />', {
        text: e.test1 + ' + ' + e.test2 + ' + ' + e.test3 + ' / = ' + average
    }).appendTo('body');

    e.average = average;
});

console.log(dataArray);

http://jsfiddle.net/z6FGg/

4m1r
  • 12,234
  • 9
  • 46
  • 58