score:0

Accepted answer

Basically you can take Array#map() for this task.

var data0 = [{ id: 'name1', value1: 5, value2: 13 }, { id: 'name2', value1: 2, value2: 5 }, { id: 'name3', value1: 4, value2: 6 }, { id: 'name4', value1: 3, value2: 7 }],
    result = function (array) {
        var sum1 = 0,
            sum2 = 0,
            r = array.map(function (a) {
                sum1 += a.value1;
                sum2 += a.value2;
                return { id: '_' + a.id, value: a.value2 - a.value1 };
            });
        return [{ id: 'sum1', value: sum1 }].concat(r, { id: 'sum2', value: sum2 });
    }(data0);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

score:-1

There are many different ways to achieve the result you need. One of them is to use map and reduce functions on the array items. You can read more about map and reduce on MDN .

var  data0 = [ {id: 'name1', value1: 5, value2: 13}, {id: 'name2', value1: 2, value2: 5}, {id: 'name3', value1: 4, value2: 6}, {id: 'name4', value1: 3, value2: 7} ];

// calculate `sum1` and `sum2` as the sums of `value1` and `value2` respectively
var sums = data0.reduce(function (sums, item) {
    sums.sum1.value += item.value1;
    sums.sum2.value += item.value2;
    return sums;
}, {
    sum1: {id: 'sum1', value: 0},
    sum2: {id: 'sum2', value: 0}
});

// transform (map) the existing data and calculate `value` as a difference between `value2` and `value1`
var restOfData = data0.map(function (item) {
    return {
        id: item.id,
        value: item.value2 - item.value1
    };
});

// merge the obtained results into a single array
var data = [sums.sum1].concat(restOfData, [sums.sum2]);

document.write(JSON.stringify(data));

This code is even more readable if you're using ES2015 thanks to arrow functions and spread operator: http://codepen.io/mmiszy/pen/EKmmEy?editors=0010

score:0

data0 = [ {id: name1, value1: 5, value2: 13}, {id: name2, value1: 2, value2: 5}, {id: name3, value1: 4, value2: 6}, {id: name4, value1: 3, value2: 7} ];
var data = [];
var sum1 = 0;
var sum2 = 0; 
for (i = 0; i < data0.length; i++) {
  sum1 += data0[i].value1;
  sum2 += data0[i].value2;
  var diff = data0[i].value2 - data0[i].value1;
  data.push({id: 'name' + (i+1) + '_', value: diff});
}
data.unshift({id: 'sum1', value: sum1});
data.push({id: 'sum2', value: sum2});

Array.push() adds after the last Item of the Array, Array.unshift() before the first Item.

score:1

Wasn't sure what your id's were to be as it looked like you were using variables. I converted them into strings in this case. Enjoy!

Code could have been compact but i thought this would make it more readable for you in future. Feel free to ask any further questions, happy to help!

var data = [ 
  {
    id: 'name1', 
    value1: 5, 
    value2: 13
  }, 
  {
    id: 'name2', 
    value1: 2, 
    value2: 5
  }, 
  {
   id: 'name3', 
   value1: 4, 
   value2: 6
  }, 
  {
    id: 'name4', 
    value1: 3, 
    value2: 7
  } 
];

// Calculate sums
var value1Sum = 0;
var value2Sum = 0;

data.forEach(function(object) {
  value1Sum += object.value1;
  value2Sum += object.value2;
});

// Insert sums at front and back
data.unshift({
  id: 'sum1',
  value: value1Sum
});

data.push({
  id: 'sum2',
  value: value2Sum
});

// Calculate differences
data = data.map(function(object, idx) {
  if (idx === 0 || idx === data.length - 1) {
    return object;
  }
  
  return {
    id: object.id + '_',
    value: object.value2 - object.value1
  };
});


Related Query

More Query from same tag