简体   繁体   English

在for循环javascript中推送数组

[英]Pushing an array in a for loop javascript

I have 2 arrays I am trying to push, one called potential Word and one allword. 我要尝试推送2个数组,一个数组称为“潜在单词”,另一个称为“ allword”。 allword is a global, and potential is in the function.I only want to push allword on the first pass of the code, so I have the variable pusher, so it will not run again after one run. allword是全局变量,函数中有潜力。我只想在代码的第一遍上推送allword,所以我有变量pusher,因此一次运行后不会再次运行。 Only potentialWord will. 只有潜力词会。

the problem is that allwords never sends any data back, while potential word does Even when pusher is 0. 问题在于,即使pusher为0,所有字也不会发送任何数据,而潜在字会发送任何数据。

for (var i = 0; i < info.length; i++) {
  potentialWord.push(info[i].word);
  while (pusher = 0){
    allwords.push(info[i].word);
  }
}
pusher +=1;

I would like for info to be pushed to both arrays, then after my function is called again, only potential word is pushed while allwords remains the same. 我希望将信息推送到两个数组,然后再次调用我的函数后,仅推送潜在单词,而所有单词保持不变。

Thank You! 谢谢!

Typo here 错字在这里

while (pusher = 0)

Should be 应该

while (pusher === 0)

or 要么

while (pusher == 0)

Explanation 说明

You should be doing a comparison, which is using === or == . 您应该进行比较,使用=====

Instead the typo used assignment, which is = . 相反,错字使用的分配是=

while (pusher = 0) will always returns falsy. while (pusher = 0)将始终返回falsy。 Because pusher = 0 expression returns 0 , which will be evaluated to false in JavaScript. 因为pusher = 0表达式返回0 ,所以在JavaScript中它将被评估为false

Therefore, the while loop has never run. 因此,while循环永远不会运行。 So, allwords is empty. 因此,所有allwords都是空的。

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

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