for in for of for each 区别
时间: 2023-10-20 19:30:19 浏览: 90
for循环和for...in循环是用于遍历对象的属性和数组的索引,而for...of循环是用于遍历可迭代对象的元素。
for循环是最常见的循环结构,它可以按照指定的条件重复执行一段代码。在for循环中,我们需要指定一个初始值、一个终止条件和一个每次迭代后执行的操作。
for...in循环用于遍历一个对象的可枚举属性,它会将对象的每个属性名称赋值给指定的变量,并执行相应的操作。通常用于遍历对象而不是数组。
for...of循环用于遍历可迭代对象(如数组、字符串、Set、Map、Generator等)中的元素。它会将对象的每个元素赋值给指定的变量,并执行相应的操作。
for each循环在JavaScript中已经被废弃,不再使用。
相关问题
for each和for
in Javascript有什么区别?
for each是JavaScript的一个老特性,它可以遍历一个数组、类数组或者对象的可枚举属性,但是现在已经被废弃了,不再是JavaScript的标准语法。
for in则可以遍历一个对象的所有属性名(包括可枚举和不可枚举属性),但是由于它是遍历对象属性名的语法,对于数组的遍历并不适用,适用的是对象的遍历。所以如果要遍历数组的话,推荐使用for of循环。
Implement fastest_words, which returns which words each player typed fastest. This function is called once both players have finished typing. It takes in a game. The game argument is a game data abstraction, like the one returned in Problem 9. You can access words in the game with selectors word_at, which takes in a game and the word_index (an integer). You can access the time it took any player to type any word using the time function provided in cats.py. The fastest_words function returns a list of lists of words, one list for each player, and within each list the words they typed the fastest. In the case of a tie, consider the earliest player in the list (the smallest player index) to be the one who typed it the fastest. Be sure to use the accessor functions for the game data abstraction, rather than assuming a particular data format.其中game函数如下def game(words, times): """A data abstraction containing all words typed and their times.""" assert all([type(w) == str for w in words]), 'words should be a list of strings' assert all([type(t) == list for t in times]), 'times should be a list of lists' assert all([isinstance(i, (int, float)) for t in times for i in t]), 'times lists should contain numbers' assert all([len(t) == len(words) for t in times]), 'There should be one word per time.' return
Sure, I can help you with that. Here's the implementation of the function fastest_words():
```
def fastest_words(game):
num_players = len(game.times)
num_words = len(game.words)
player_words = [[] for _ in range(num_players)]
for word_index in range(num_words):
fastest_player = 0
fastest_time = game.time(0, word_index)
for player_index in range(1, num_players):
time = game.time(player_index, word_index)
if time < fastest_time:
fastest_player = player_index
fastest_time = time
player_words[fastest_player].append(game.word_at(word_index))
return player_words
```
This function takes in a game data abstraction and returns a list of lists of words, one list for each player, and within each list the words they typed the fastest. The function first initializes an empty list for each player to store their fastest words. It then loops through each word in the game and finds the player who typed the word the fastest. If there's a tie, the function considers the earliest player in the list to be the one who typed it the fastest.
I hope this helps! Let me know if you have any other questions.
阅读全文