简体   繁体   English

我试图获得在 Javascript 中添加数字数组的结果,但它显示的是 Nan 结果

[英]I am trying to get a result of addition of number array in Javascript but its showing Nan result

// I want to add a number array elements but the result is showing Nan why so // 我想添加一个数字数组元素,但结果显示 Nan 为什么要这样

let numarr = [5598, 4589 ,25465164]
    let len = numarr.length;
    let i,  numval = 0;
    for(i=0; i <= len; i++) {
      numval +=  numarr[i];
    } 
    document.getElementById("demo").innerHTML = numval;

It is because of the <= in for-loop .这是因为for-loop中的<= If you use <= then loop will run past the length of the array and it will try to access the element that is not present.如果您使用<=则循环将运行超过数组的长度,并且它将尝试访问不存在的元素。

If you are trying to access the element that is not present then it will return undefined .如果您尝试访问不存在的元素,那么它将返回undefined Addition of a number with undefined will give you NaN添加一个undefined的数字会给你NaN

Either use numarr.length - 1 or i < len使用numarr.length - 1i < len

 let numarr = [5598, 4589, 25465164] let len = numarr.length; let i, numval = 0; for (i = 0; i < len; i++) { numval += numarr[i]; } document.getElementById("demo").innerHTML = numval;
 <h1 id="demo"></h1>

Alternate solution: You can also use reduce here to achieve the same result替代解决方案:您也可以在此处使用 reduce 来获得相同的结果

 let numarr = [5598, 4589, 25465164] const numval = numarr.reduce((a, c) => a + c); document.getElementById("demo").innerHTML = numval;
 <h1 id="demo"></h1>

Your array has 3 elements at indices 0, 1 and 2. So your arrays should run from 0 to array.length - 1 .您的数组在索引 0、1 和 2 处有 3 个元素。因此您的 arrays 应该从 0 到array.length - 1 If you tries to access numarr[3] the value will be undefined .如果您尝试访问numarr[3] ,则该值将为undefined Adding undefined to a number is NaN that means not a numberundefined添加到数字是NaN表示不是数字

It should be i < len应该是i < len

 let numarr = [5598, 4589, 25465164]; let len = numarr.length; let i, numval = 0; for (i = 0; i < len; i++) { numval += numarr[i]; } document.getElementById("demo").innerHTML = numval;
 <div id="demo"></div>

The mistake you made has already been pointed out in numerous other answers, I would like to show you an alternative in modern JS.您犯的错误已经在许多其他答案中指出,我想向您展示现代 JS 的替代方案。

Instead of using an index-based for-loop, Javascript has a specialized function for what you want to do, which is called Array.prototype.reduce . Javascript 没有使用基于索引的 for 循环,而是有一个专门的 function 用于您想要执行的操作,称为Array.prototype.reduce It is called once for each array element, adds its value to the accumulator and returns the accumulator after the last iteration.它为每个数组元素调用一次,将其value添加到accumulator并在最后一次迭代后返回累加器。 The start value 0 for the accumulator is the second parameter.累加器的起始值0是第二个参数。

 const numarr = [5598, 4589,25465164] const sum = numarr.reduce((accumulator, value) => accumulator+value, 0); document.getElementById("demo").innerHTML = sum;
 <div id="demo"></div>

i should be smaller than len not smaller or equal . i应该小于lensmaller or equal

Example:例子:

 let numarr = [5598, 4589,25465164] let len = numarr.length; let i, numval = 0; for(i=0; i < len; i++) { numval += numarr[i]; } console.log(numval)

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

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