简体   繁体   English

我如何遍历数组并仅求和一半项? (JavaScript的)

[英]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. 假设我有一个由十个项目组成的数组,我想制作2个单独的数组,以奇数和偶数分布将原始项中的项目相加。 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" :) 特别感谢@Dana Woodman,您对push()函数的使用是“我的难题中缺少的部分” :)

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: 或使用一个Array.prototype.reduce调用:

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. 使用带mod检查的for循环来检查偶数或奇数索引。 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. 在这里使用按位OR |0可确保sum [-1]中的未定义值是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 样式说明:在变量上使用Pascal Case通常被认为是不好的做法,除非它们是类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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