java
html
php
iphone
xml
ajax
linux
objective-c
visual-studio
multithreading
flash
json
delphi
apache
mvc
php5
api
jsp
postgresql
dom
for ... in iterates over keys, not elements. So for(obj in testObjects) means obj is the key, which in this case is 0 (because it is an array).
for
in
for(obj in testObjects)
obj
You could do
for (key in testObjects) { alert(testObjects[key].HI) }
However, this is not a good practice. If you do it this way and somebody adds a method to testOjbects or to all arrays, it will iterate over that method as well.
testOjbects
Your testObjects variable is actually an array, not an object. Thus it obviously doesn’t have anything under the "HI" key (also, you are not supposed to use for…in loops for arrays in JavaScript; use regular for or while loops instead).
testObjects
for…in
while
Either change your loop code to this: for (obj in testObjects[0]), or the variable definition to this testObjects = { "HI" : "how are you" }.
for (obj in testObjects[0])
testObjects = { "HI" : "how are you" }
If your program does expect an array of objects, you’d need to set up two nested loops, probably like this:
for (var i=0, l=testObjects.length; i<l; i++) { for (var key in testObjects[i]) { // console.log(key + ' is ' + testObjects[i][key]) } }