I have an array with objects inside of it.
var obj1 = [
user1 = {
"name" : "John",
"age" : 24,
"city" : "London"
},
user2 = {
"name" : "Jane",
"age" : 22,
"city" : "New York",
}
];
Now what I would like to do is to get name of the objects inside the array so in this case it would be "user1" and "user2".
FIRST TRY
At first I tried doing it like this:
for(i = 0; i < obj1.length; i++){
document.getElementById('app').innerHTML += obj1[i];
}
And the outcome is [object Object][object Object]
SECOND TRY
So then I tried to use JSON.strigify
on the objects, but then it returns the whole object and not it's name as I would like to:
for(i = 0; i < obj1.length; i++){
document.getElementById('app').innerHTML += JSON.stringify(obj1[i]);
}
Outcome:
{"name":"John","age":24,"city":"London"}{"name":"Jane","age":22,"city":"New York"}
QUESTION
Is it possible, and if yes how should I go about displaying names of all of the objects inside of an array, but not displaying their content. Desired outcome:
user1 user2