If i have an array of hashes, whats the best way to iterate?
var a = [{"a": "1"}, {"b": "2"}, {"c": "3"}]
for(var i in a) {
console.log(a[i]) //prints each hash
console.log(i) //prints the index
}
If i want to get a,b,c or 1,2,3 whats the best way?
Thanks
for (var i in a)in Javascript because that iterates all enumerable properties ofawhich can include more items than just array elements.var a = {"a": "1", "b": "2", "c": "3"}and then you can just get the keys withObject.keys(a)and then get any value witha[key].[{a: "1"}, {b: "2"}, {c: "3"}]is very valid JavaScript. The keys in an object literal can be identifier names, strings or numbers.PropertyNamecan either be aIdentifierName(e.g.foo), aStringLiteral(e.g.'foo'or"foo") or aNumericLiteral(e.g.42).{ unknownprop: "value" }would be invalid JSON, but is a valid JS object literal. And yes, in this context, "works" means "valid" for me, because "invalid" JavaScript would throw a syntax error.