Pages

Wednesday, July 23, 2014

Get all property names of javascript object

This is a tricky business and I believe I had tried this multiple times and ended using a work around. I had a javascript object in json notation and it had a lot of values encapsulated in it. To be specific it looked like the following

 

>> window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US")

{
       HSI B MAKATI PHILIPPINES : [object Object],
       HSI B HEREDIA COSTA RICA : [object Object],
       FSC-HEREDIA, COSTA RICA : [object Object],
       HSI QUEZON CITY PHILIPPINES : [object Object],
       CSSC-VENDOR-TELETECH, NOVA, PH : [object Object],
       HSI C HYDERABAD INDIA : [object Object],
       HSI C NOVALICHES PHILIPPINES : [object Object],
       EVRC . PASIG, PHILIPPINES : [object Object],
       HSI B TIJUANA MEXICO : [object Object],
       FSC-HYDERABAD,INDIA : [object Object]
}

All I wanted to do was get an array of the property names. Following attempts were futile. They either gave empty values or errors

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x[0]

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x(0)

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x["0"]

>> window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US").properties

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x[0]

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x.length

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x.size

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x.size()

"Object doesn't support property or method 'size'"

 

Only Specific calls like the following with the property names worked. But this is no good to me as I cant always no all the properties present inside the object

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US");x["FSC-HYDERABAD,INDIA"]

{
       Name : "FSC-Hyderabad,India",
       Description : "Refers to the Center corresponding to the code FSC-Hyderabad,India",
       Value : "AR",
       Type : "AGENT_CENTER_OUTSIDE_US"
}

 

At last the following attempts helped me

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US"); for(y in x){y}

"FSC-HYDERABAD,INDIA"

>> x=window.top.getGlobalParamData("AGENT_CENTER_OUTSIDE_US"); for(y in x){console.log(y)}

LOG: HSI B MAKATI PHILIPPINES

LOG: HSI B HEREDIA COSTA RICA

LOG: FSC-HEREDIA, COSTA RICA

LOG: HSI QUEZON CITY PHILIPPINES

LOG: CSSC-VENDOR-TELETECH, NOVA, PH

LOG: HSI C HYDERABAD INDIA

LOG: HSI C NOVALICHES PHILIPPINES

LOG: EVRC . PASIG, PHILIPPINES

LOG: HSI B TIJUANA MEXICO

LOG: FSC-HYDERABAD,INDIA

 

Now I can construct my required array J

 

Hope this helps some one J

No comments:

Post a Comment