admin管理员组文章数量:1134247
I have an object (an "associate array" so to say - also known as a plain JavaScript object):
obj = {}
obj["Foo"] = "Bar"
obj["bar"] = "Foo"
I want to iterate over obj
using CoffeeScript as follows:
# CS
for elem in obj
bu the CS code above compiles to 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?
I have an object (an "associate array" so to say - also known as a plain JavaScript object):
obj = {}
obj["Foo"] = "Bar"
obj["bar"] = "Foo"
I want to iterate over obj
using CoffeeScript as follows:
# CS
for elem in obj
bu the CS code above compiles to 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?
4 Answers
Reset to default 354Use 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. 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).
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
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 :
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 :
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"
for key, val of arr
console.log key + ': ' + val
which returns :
Foo : Bar
Bar : Foo
本文标签: javascriptHow to iterate over the keys and values in an object in CoffeeScriptStack Overflow
版权声明:本文标题:javascript - How to iterate over the keys and values in an object in CoffeeScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736789422a1953022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
length
property that simply refers to the highest numerical index (plus 1). What you want is just an "object":obj = {}
. Arrays are objects, but there's no reason to use one in your example. – Trevor Burnham Commented Jun 20, 2011 at 15:15