简体   繁体   中英

How can I loop through array and sum only half the items? (javascript)

say I have an array of ten items, and I want to make 2 separate arrays which sums the items inside the original one by odd and even distribution. For example:

OriginalArray [288,333,313,296,102,299,333,333,316,289]
arraySumA [288,288+313,288+313+102,288+313+102+333,288+313+102+333+316]
arraySumB [333,333+296,333+296+299,333+296+299+333,...]

how can I do that using no more than one loop (if possible)?

Update: thanks all of you guys for your suggestions, I didn't use them eventually but they've certainly helped me devise my own solution. Especially thanks to you @Dana Woodman your use of push() function was "the missing piece in my puzzle" :)

var arraySumA = OriginalArray.reduce(function(c, x, i) {
    if(i % 2==1) c += x;
    return c;
});

var arraySumB = OriginalArray.reduce(function(c, x, i) {
    if(i % 2==0) c += x;
    return c;
});

Or with one Array.prototype.reduce call:

var sums = OriginalArray.reduce(function(c, x, i) {
    if(i % 2==0) c.even += x;
    else c.odd += x;
    return c;
}, {even: 0, odd: 0});

Use a for loop with a mod check for even or odd indices. Push the sum of the relevant array into the next value of the relevant array.

for(var i = 0; i < original.length; i++){
  if( i % 2 == 0 ){
    sumA.push(original[i]+(sumA[sumA.length-1]|0));
  }else{
    sumB.push(original[i]+(sumB[sumB.length-1]|0));
  }
}

Using the bitwise OR |0 here ensures that an undefined value from sum[-1] is 0.

 var original = [288,333,313,296,102,299,333,333,316,289]; var sumA = [], sumB = []; for(var i = 0; i < original.length; i++){ if( i % 2 == 0 ){ sumA.push(original[i]+(sumA[sumA.length-1]|0)); }else{ sumB.push(original[i]+(sumB[sumB.length-1]|0)); } } $("#a").text(JSON.stringify(sumA)); $("#b").text(JSON.stringify(sumB)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="a"></div> <div id="b"></div> 

This should do what you're looking for with only one loop:

var OriginalArray = [288,333,313,296,102,299,333,333,316,289];
var arraySumA = [];
var arraySumB = [];

// You use any type of iterator here, like a standard for-loop as well.    
OriginalArray.forEach(function (num) {
  if (num % 2 === 0) {
    // Sum of even items
    var prev = arraySumA[arraySumA.length - 1] | 0;
    arraySumA.push(prev + num);
  } else {
    // Sum of odd items
    var prev = arraySumB[arraySumB.length - 1] | 0;
    arraySumB.push(prev + num);
  }
});

/*
Results in:
arraySumA = [288, 584, 686, 1002]
arraySumB = [333, 646, 945, 1278, 1611, 1900]
*/

If the sum of the previous is the sum of the existing array then you should just have to add the previous item to the current one.

Style note: it's generally considered bad practice to use Pascal Case on variables unless they're classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM