【问题标题】:Loop through then reset an array in node.js循环然后重置 node.js 中的数组
【发布时间】:2021-03-19 02:30:10
【问题描述】:

编辑:我尝试过类似的事情:

let firstarray = ["one", "two", "three", "four", "five", "six"]
let secondarray = ["1", "2", "3"]
let currentindex = 0;
if(currentindex == secondarray.length) {
currentindex - secondarry.length
}
firstarray.forEach(item => {
console.log(`${item} ${secondarray[currentindex]}`)
currentindex++
});

这行得通吗?或者有什么类似的我可以做的吗?

简单解释:

//I have 2 arrays:
let firstarray = ["one", "two", "three", "four", "five", "six"]
let secondarray = ["1", "2", "3"]
//how can i log both of these but repeat through the second array as such:
"one 1"
"two 2"
"three 3"
"four 1"
"five 2"
"six 3"

不是那么简单的解释:

我想尝试反复循环遍历一个数组,例如:

let myarray = ["1", "2", "3"] //multiple strings

上面是我的数组,我知道如果我这样做:

console.log(myarray[0])

它返回 1,如果我这样做:

myarray.forEach(item => {
console.log(item)
});

它记录 1 然后 2 然后 3 但这取决于数组本身。

假设我有另一个包含 6 个项目的数组:

let firstarray = ["one", "two", "three", "four", "five", "six"]
let secondarray = ["1", "2", "3"]
 firstarray.forEach(item => {
console.log(`${item}`)
});

这个日志:

one
two
three
four
five
six

我怎样才能让它记录并循环第二个数组,所以它返回这个:

"one 1"
"two 2"
"three 3"
"four 1"
"five 2"
"six 3"

【问题讨论】:

    标签: javascript node.js arrays foreach numbers


    【解决方案1】:

    你可以使用第二个数组长度的模数...

    firstarray.forEach((num, index) => {
        console.log(num + " " + secondarray[index % secondarray.length])
    })
    

    输出:

    "one 1"
    "two 2"
    "three 3"
    "four 1"
    "five 2"
    "six 3"
    

    【讨论】:

    • 所以我猜想将第二个值存储为变量然后:let secondvalue = secondarray[index % secondarray.length] 会工作吗?
    【解决方案2】:

    如果你想对任意 2 个数组做这个(不知道哪个更大)

    //get the biggest array's length
    var maxLen = Math.max(arr1.length, arr2.length);
    for(var i = 0;i<maxLen;i++) {
        //get the elements mod the length to wrap around
        var ele1 = arr1[i % arr1.length];
        var ele2 = arr2[i % arr2.length];
        console.log(ele1 + " " + ele2);
    }
    

    【讨论】:

      【解决方案3】:

      使用单个索引遍历两个数组,将该索引的剩余部分用于第二个数组。

      let firstarray = ["one", "two", "three", "four", "five", "six"]
      let secondarray = ["1", "2", "3"]
      let index = -1
      while (++index < firstarray.length) {
        console.log(firstarray[index], secondarray[index % secondarray.length])
      }

      【讨论】:

      • 有没有办法在 foreach 内部严格执行此操作:let firstarray = ["one", "two", "three", "four", "five", "six"] let secondarray = ["1", "2", "3"] firstarray.forEach(item =&gt; { //everything done here });
      【解决方案4】:

      我想通了!

      let firstarray = ["one", "two", "three", "four", "five", "six"]
      let secondarray = ["1", "2", "3"]
      let currentindex = 0;
      firstarray.forEach(item => {
      console.log(`${item} ${secondarray[currentindex]}`)
      currentindex++
      if(currentindex === secondarray.length) {
        currentindex -= secondarray.length
      }
      });
      

      返回我想要的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-14
        • 1970-01-01
        • 2017-07-06
        • 2014-10-15
        • 2014-11-09
        • 1970-01-01
        • 1970-01-01
        • 2017-10-06
        相关资源
        最近更新 更多