简体   繁体   English

我们如何在 JavaScript 中使用 foreach 循环增加变量计数

[英]how do we increasing variable count using a foreach loop in JavaScript

I am trying to run a foreach loop to keep looping into the var number is reached.我正在尝试运行一个 foreach 循环以保持循环进入 var 编号。 I also need to keep track of the index.我还需要跟踪索引。 so far what I have tried到目前为止我已经尝试过

const data = ()=>{
    const myNumber = input.getPortCount() // Example currently this will be output a number 0 - 9
    foreach(i == 0, i => myNumber ++i){
         return console.log('i equals', i)
    }
}

I need I to increase by 1 start at 0 into it equal to the number of the variable myNumber我需要我从 0 开始增加 1 到它等于变量 myNumber 的数量

You should go learn about loops.您应该 go 了解循环。 You need here the for loop and not foreach .... but you have more mistakes.你在这里需要for循环而不是foreach .... 但你有更多的错误。 here is the loop you need:这是您需要的循环:

for (var i = 0; i <= myNumber; ++i){
     console.log('i equals', i);
}

This is syntax for forEach array, used on arrays:这是forEach数组的语法,用于 arrays:

array.forEach(function(currentValue, index, arr), thisValue)

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

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