简体   繁体   English

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

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

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 obj using CoffeeScript as follows: 我想使用CoffeeScript迭代obj ,如下所示:

# 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中做到这一点?

Use for x,y of L . for x,y of L Relevant documentation . 相关文件

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 ages as mentioned by Aaron Dufour in the comments. 您可能还想考虑Aaron Dufour在评论中提到的for own k,v of ages的变体。 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. 这会添加一个检查以排除从原型继承的属性,这可能不是本例中的问题,但可能是在构建其他东西之上。

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 

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 “comprehensions替换(并编译成)for循环,带有可选的guard子句和当前数组索引的值。与for循环不同,数组理解是表达式,可以返回和分配。”, http://coffeescript.org/ #loops

with your convention, arr is an array, but "foo" is a property of this array, it is not an indexed value. 使用您的约定,arr是一个数组,但“foo”是此数组的属性,它不是索引值。 If you want to store your data the indexed values of an array, you should have written : 如果要将数据存储为数组的索引值,则应该写入:

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 data in array”

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

which returns : 返回:

Foo : Bar
Bar : Foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM