Reputation: 623
I have an object which looks like this:
var room = {"a":
[
{room: "1.2.1"},
{room: "1.2.2"},
{room: "1.2.3"}
],
1.2: [
{room: "1.3.1"},
{room: "1.3.2"},
{room: "1.3.3"}
]};
I only want to read the object's properties which are a
and 1.2
and put it into a variable.
For example:
var oname = room.getName();
output:
a
1.2
Upvotes: 0
Views: 121
Reputation: 116
Something like this?
function getRoomName(r){
var names="";
for (var prop in r) {
names+=prop+"\n";
}
return names;
}
console.log(getRoomName(room));
Upvotes: 2
Reputation: 22697
use Object.keys()
var keys = Object.keys(room)
Then keys
will have ["a","1.2"]
as value.
Also, you have a dict where its values are arrays, not an array itself.
Upvotes: 4
Reputation: 6766
you can also try like following.
In Below snippet for in loop will iterate over object properties.
var room = {"a":
[
{room: "1.2.1"},
{room: "1.2.2"},
{room: "1.2.3"}
],
1.2: [
{room: "1.3.1"},
{room: "1.3.2"},
{room: "1.3.3"}
]};
$(function(){
for(obj in room)
{
//debugger;
document.write(obj)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1