简体   繁体   中英

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

The output object from the console.log part is right, but also in this line is the TypeErro above. I think, the "j" does not replace the number, but in the output console it works...

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

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

loops to iterate over your Array.

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