简体   繁体   English

JavaScript While循环:变量不会替换为

[英]JavaScript While loop: variable does not replace with

I have the following code: 我有以下代码:

j = 1;
while (j < 50) {
  //requirement:
     console.log(JSONitem [j]["criteria"]);
     reqtest = Object.keys(JSONitem [j]["criteria"]);
         ....
     j++;
}

But when I execute this, I get the following error: 但是,当我执行此操作时,出现以下错误:

TypeError: JSONitem[j] is undefined TypeError:JSONitem [j]未定义

The output object from the console.log part is right, but also in this line is the TypeErro above. console.log部分的输出对象是正确的,但在这一行中也是上面的TypeErro。 I think, the "j" does not replace the number, but in the output console it works... 我认为,“ j”不会替换数字,但是在输出控制台中它可以工作...

Thanks! 谢谢!

console.log(...JSONitem.map(el=>el.criteria));

Your code may behaves unexpected if the length of your array (?) changes. 如果数组(?)的长度发生变化,您的代码可能会表现出异常。 You should use the Array.prototype methods or the for..in, for..of or 您应该使用Array.prototype方法或for..in,for..of或

for(i=0;i<array.length;i++) 

loops to iterate over your Array. 循环遍历数组。

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

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