im sending http request (using ajax) to my sever, and it returns json string something like :
{
"neighbors":{
"data":[
{
"id":"7f40cb24-603e-4943-9a0b-e16e024c8bd5",
"name":"jeff ferry",
"picture":"url",
"location":{
"data":[
{
"latitude":"xxx",
"longitude":"yyy"
}
]
}
}
]
}
}
and in html page ajax callback im accessing data like :
success: function (data) {
var outerObj = data.neighbor.data;
var res = "";
for (i in outerObj )
{
if (outerObj .hasOwnProperty(i)) {
// my outer stuff
var innerObj = outerObj.data;
for (j in innerObj)
{
// my inner stuff
if (innerObj.hasOwnProperty(j)) {
}
}
}
}
}
but im wondering if i can access it by some optimized(shorter) way ?
any help appreciable
Maybe you could tighten up the data structure returned from the server? Since 1) the location property is really just a lat/long tuple, and 2) the object around the inner
dataarray is just a redundant wrapper, how about this:That way, your JavaScript becomes something like: