Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中迭代特定索引?_Javascript_Python - Fatal编程技术网

如何在Javascript中迭代特定索引?

如何在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

我试图找出如何在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[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)
});