如何在Javascript中迭代特定索引?
我试图找出如何在javascript中只遍历列表的特定部分 在Python中,我这样做:如何在Javascript中迭代特定索引?,javascript,python,Javascript,Python,我试图找出如何在javascript中只遍历列表的特定部分 在Python中,我这样做: board = range(101) answer = 92 match = [] low = answer - 2 high = answer + 2 for i in board[low:high + 1]: match.append(i) for (let index = low; index < (high + 1); index++) { match.push(board[i
board = range(101)
answer = 92
match = []
low = answer - 2
high = answer + 2
for i in board[low:high + 1]:
match.append(i)
for (let index = low; index < (high + 1); index++) {
match.push(board[index])
}
我的问题是如何用javascript编写一个类似的for循环?您可以在列表的各个部分进行迭代
const board = new Array(101).fill().map((_, i) => i) //this way you can create range of [0..101]
...
board.slice(low, high+1).forEach(i=>{
match.append(i)
})
如果您的目标是存档以下内容的
匹配结果
:
for i in board[low:high + 1]:
match.append(i)
只需使用:
但是如果你的目标是产生同样的努力(做一个循环),你可以使用以下任何一种技术:
您可以这样做:
board = range(101)
answer = 92
match = []
low = answer - 2
high = answer + 2
for i in board[low:high + 1]:
match.append(i)
for (let index = low; index < (high + 1); index++) {
match.push(board[index])
}
甚至使用slice和forEach:
也可以使用:
中…的错误。你是说?非常感谢。这些不同的方法非常有用
for (let item in board.slice(low, high + 1)) {
match.push(item)
}
board.slice(low, high + 1).forEach(function(item){
match.push(item)
});
board.slice(low, high +1).forEach((i) = {
match.push(i)
});