jQuery accessing objects

Posted by user1275268 on Stack Overflow See other posts from Stack Overflow or by user1275268
Published on 2012-06-13T04:21:05Z Indexed on 2012/06/13 4:40 UTC
Read the original article Hit count: 93

I'm trying to access the values of an object from a function I created with a callback, but have run into some trouble. I'm still fairly new at jQuery/javascript.

I call the function as follows:

siteDeps(id,function(data){
    $.each(data,function(key,val) {
        console.log(key);
        console.log(val);
    });
});

The function runs 5 ajax queries from XML data and returns data as an multidimensional object; here is a excerpt showing the meat of it:

function siteDeps(id,callback) {
    var result = { 
        sitecontactid : {}, 
        siteaddressid : {}, 
        sitephoneid : {}, 
        contactaddressid : {}, 
        contactphoneid : {} 
    };

...//....

    var url5 = decodeURIComponent("sql2xml.php?query=xxxxxxxxxxx");             
    $.get(url5, function(data){
        $(data).find('ID').each(function(i){
            result.delsitephoneid[i] = $(this).text(); 
        }); 
    }); 
    callback(result);
}

The console.log output shows this:

sitecontactid
Object
    0: "2"
    1: "3"
    __proto__: Object
siteaddressid
Object
    0: "1"
    __proto__: Object
sitephoneid
Object
    0: "1"
    1: "5"
    2: "54"
    __proto__: Object
contactaddressid
Object
    0: "80"
    __proto__: Object
contactphoneid
Object
    0: "6"
    __proto__: Object

How can I extract the callback data in a format I can use, for instance sitephoneid: "1","5","54"

Or is there a better/simpler way to do this?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery