1

How to loop through the Restfulwebserivice using for loop with a single object. and I don't want to use for each loop so how do i do this. i am not able to get the data by using

  for(var key in data) {
                    alert(data[key].hotel_geo_node.name);
             if(data.hasOwnProperty(key)){
           }

and here is the api { "data": { "4325474491990470056": { "hotel_geo_node": { "name": "The Capitol", "tags": { "hotel_chain_code": [ "nichendgrp" ], "property_type": [ "Hotel" ], "property_budget_category": [ "Luxury" ], "others": [ "goibibo_hotel" ] },

    }, 

   "4325474491990470057": {
        "hotel_geo_node": {
            "name": "The Capitol", 
            "tags": {
                "hotel_chain_code": [
                    "nichendgrp"
                ], 
                "property_type": [
                    "Hotel"
                ], 
                "property_budget_category": [
                    "Luxury"
                ], 
                "others": [
                    "goibibo_hotel"
                ]
            }, 

    }, 
    "4325474491990470058": {
        "hotel_geo_node": {
            "name": "The Capitol", 
            "tags": {
                "hotel_chain_code": [
                    "nichendgrp"
                ], 
                "property_type": [
                    "Hotel"
                ], 
                "property_budget_category": [
                    "Luxury"
                ], 
                "others": [
                    "goibibo_hotel"
                ]
            }, 
    }
 }

}

hit
  • 25
  • 5

1 Answers1

0

You cannot loop through object's keys using "standard" for loop since it is not array and it's keys are not numbers. You can only loop throw it using for-in loop:

for(var key in data) {
    if(data.hasOwnProperty(key)){ // check if 'key' property is in object 'data' and not in it's prototype chain
        doTheStuff(data[key]);
    }
}
Roman Bekkiev
  • 3,010
  • 1
  • 24
  • 31
  • hi roland, i need get every object just like we get every array and loop through that array i want the same thing. how do i do that . :( – hit Jan 19 '15 at 19:05
  • Sorry, it is not clear what do you want. If you want to rich that objects named like `"4325474491990470058", you'll get it in the line 3 of my snippet as `data[key]`. Or if you wanna get all nested objects you can do it using recursion function similar to that one in this answer: http://stackoverflow.com/a/11311206/1105860 – Roman Bekkiev Jan 19 '15 at 22:00
  • okay!! so how do i get "name": "The Capitol" from this web service. please let me know. – hit Jan 20 '15 at 07:59
  • since it's looks like API you can just get `data[key].hotel_geo_node.name` – Roman Bekkiev Jan 20 '15 at 09:27
  • Hi Roland, I am getting this error TypeError: data[key].hotel_geo_node is undefined . – hit Jan 20 '15 at 10:56
  • Hey, bro, who's programming that stuff - you or me? ;-) – Roman Bekkiev Jan 20 '15 at 15:23
  • programing is doing by me but i tried all the things but couldn't get the name from all object . don't know why i am getting this error . – hit Jan 20 '15 at 15:32
  • use chrome dev.tools, go to debugger, turn on 'stop on exceptions', check variables, check your object. Investigate. – Roman Bekkiev Jan 20 '15 at 16:45