I am calling this on an array with 3 objects in it. It ends up returning the correct keys in addition to these extra keys...
unique
last
truncate
random
include
contains
any
Why?
You're getting those extra properties because you, or a library you're using, has extended the Array
prototype. As Mike points out in his answer, you can skip those by using hasOwnProperty
. Indeed, CoffeeScript has an own
keyword built in that does this for you:
for own i of foo
obj = foo[i]
...
But, as Mike also points out in his answer, it's more efficient to loop through an array by incrementing a counter rather than iterating over the keys. To do that, you'd use CoffeeScript's for...in
syntax:
for obj in foo
...
(If you need indices in the loop as well, you can write for obj, i in foo
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With