Javascript 如何在 CoffeeScript 中迭代对象中的键和值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6408726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:35:45  来源:igfitidea点击:

How to iterate over the keys and values in an object in CoffeeScript?

javascriptcoffeescript

提问by jhchen

I have an object (an "associate array" so to say - also known as a plain JavaScript object):

我有一个对象(可以说是“关联数组”——也称为普通 JavaScript 对象):

obj = {}
obj["Foo"] = "Bar"
obj["bar"] = "Foo"

I want to iterate over objusing CoffeeScript as follows:

我想迭代obj使用 CoffeeScript 如下:

# CS
for elem in obj

bu the CS code above compiles to JS:

但是上面的CS代码编译成JS:

// JS
for (i = 0, len = obj.length; i < len; i++)

which isn't appropriate in this case.

在这种情况下这是不合适的。



The JavaScript way would be for(var key in obj)but now I'm wondering: how can I do this in CoffeeScript?

JavaScript 的方式是for(var key in obj)但现在我想知道:我如何在 CoffeeScript 中做到这一点?

回答by Nick

Use for x,y of L. Relevant documentation.

使用for x,y of L. 相关文件

ages = {}
ages["jim"] = 12
ages["john"] = 7

for k,v of ages
  console.log k + " is " + v

Outputs

输出

jim is 12
john is 7

You may also want to consider the variant for own k,v of agesas mentioned by Aaron Dufour in the comments. This adds a check to exclude properties inherited from the prototype, which is probably not an issue in this example but may be if you are building on top of other stuff.

您可能还想考虑for own k,v of agesAaron Dufour 在评论中提到的变体。这会添加一个检查以排除从原型继承的属性,这在本示例中可能不是问题,但如果您正在构建其他内容,则可能会出现问题。

回答by kioopi

You're initializing an array, but then you're using it like an object (there is no "associative array" in js).

您正在初始化一个数组,但随后您将其用作对象(js 中没有“关联数组”)。

Use the syntax for iterating over objects (something like):

使用迭代对象的语法(类似):

for key, val of arr
  console.log key + ': ' + val 

回答by sqren

The short hand version using array comprehension, which can be used as a one-line loop.

使用数组推导式的简写版本,可用作单行循环。

console.log index + ": " + elm for index, elm of array

Array comprehension are:

数组理解是:

"Comprehensions replace (and compile into) for loops, with optional guard clauses and the value of the current array index. Unlike for loops, array comprehensions are expressions, and can be returned and assigned.", http://coffeescript.org/#loops

“理解用可选的保护子句和当前数组索引的值替换(并编译成)for 循环。与 for 循环不同,数组理解是表达式,可以返回和赋值。”, http://coffeescript.org/ #循环

回答by Benibur

with your convention, arr is an array, but "foo" is a property of this array, it is not an indexed value. If you want to store your data the indexed values of an array, you should have written :

按照您的约定,arr 是一个数组,但“foo”是该数组的一个属性,它不是索引值。如果要将数据存储为数组的索引值,则应该编写:

arr1 = []
arr1[0] = "Bar"
arr1[1] = "Foo"

or if you want an associative array, just use an object :

或者如果你想要一个关联数组,只需使用一个对象:

arr2 = {}
arr2["Foo"] = "Bar" // equivalent to arr2.foo="Bar"
arr2["bar"] = "Foo" // equivalent to arr2.bar="Foo"

if you want to loop over arr1 :

如果你想遍历 arr1 :

str = "values are : "
for val in arr2
  str += val + " |"
console.log key + ': ' + val

returns :

返回:

values are : Bar | Foo |

and to loop over arr2 : "for value in array"

并循环遍历 arr2 :“数组中的值”

for key, val of arr
  console.log key + ': ' + val

which returns :

返回:

Foo : Bar
Bar : Foo