# javascript - javascript - 在数组中，返回偶数的总和

``````
let numStr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const sumEvens = (numStr) => {

let sum = 0;

for (let i = 0; i < numStr.length; i++) {

if (i % 2 === 0){

sum = sum + numStr[i];

}

}

return sum;

}

console.log(sumEvens(numStr));
``````

``````
let numStr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const sumEvens = (numStr) => {

let sum = [ ];

for (let i = 0; i < numStr.length; i++) {

if (i % 2 === 0){

sum.push(numStr[i]);

}

}

return sum;

}

console.log(sumEvens(numStr));
``````

``````
if (i % 2 === 0){

``````

``````
if (numStr[i] % 2 === 0) {

``````
``````
let numStr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const sumEvens = (numStr) => {

let sum = 0;

for (let i = 0; i < numStr.length; i++) {

if (numStr[i] % 2 === 0) {

sum = sum + numStr[i];

}

}

return sum;

}

console.log(sumEvens(numStr));
``````

`filter``reduce`一起使用：

``````
let numStr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const sumEvens = arr => arr.filter(e => !(e % 2)).reduce((a, b) => a + b);

console.log(sumEvens(numStr));
``````

``````
let numStr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let sum = numStr.filter(i => i%2 ===0).reduce((a,v) => a += v);

console.log(sum);
``````

``````
let numStr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function getSum(total, num) {