【发布时间】: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