ZolaKt
ZolaKt

Reputation: 4721

jQuery .getJSON return into array variable & json array manipulation


is there any way I can get the return of $.getJSON into a variable array?
I know its async and out of scope, but I will use it inside ajax callback, I just need to get all the values first and check them against another array.

Something like:

$.getJSON('itemManager.php?a=getItems', function(data){
    // itemArray = new Array(data);
    // idsArray = new Array(data.id);
    for (var i in someOtherArray){
        if($.inArray(i, idsArray) == -1){
            // do something...
            // get jason variable by id?
            // itemArray[i].someVariable
        }
    }
}

EDIT: JSON structure

[{"id":"786","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"787","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"785","user_id":"1","seller_id":"2","address_id":"1","time":1299852114,"publicComment":null,"personalComment":null},
{"id":"784","user_id":"1","seller_id":"2","address_id":"1","time":1299852113,"publicComment":null,"personalComment":null},
{"id":"783","user_id":"1","seller_id":"2","address_id":"1","time":1299852111,"publicComment":null,"personalComment":null}]

This is basically the idea.

There are various solutions here I guess, but I'm looking for something with minimal code.

Upvotes: 5

Views: 14157

Answers (2)

Felix Kling
Felix Kling

Reputation: 816472

With the given information, there is not shortcut to test the existence of IDs. You really have to loop over everything. However you can improve a bit by creating an id => object mapping:

$.getJSON('itemManager.php?a=getItems', function(data){
    var items = {};
    for(var i = data.length; i--; ) {
        items[data[i].id] = data[i];
    }
    for (var j = someOtherArray.length; j--; ){
        var item = items[someOtherArray[j]];
        if(item){
            // do something with `item`
        }
    }
}

It woud be even better if you create this structure on the server already, then it would be:

$.getJSON('itemManager.php?a=getItems', function(data){
    for (var j = someOtherArray.length; j--; ){
        var item = data[someOtherArray[j]];
        if(item){
            // do something with `item`
        }
    }
}

You should also consider which arrays will contain more elements, data or someOtherArray and adjust your data structures such that you loop over the smaller array only.

Update:

To create the appropriate structure on the server with PHP, you have to create an associate array.

So at the point where you add an object to the array, you should not do

$items[] = $obj;

but

$items[$obj->id] = $obj; // or $obj['id'] if you have an array

Upvotes: 3

rsp
rsp

Reputation: 111366

If you get an array as your JSON response then your data variable in your callback is an array, no need to do anything with it.

If you get an object as your JSON response as the data.id in you example might suggest, and some of it's values is an array, then just use data.id as an array, or use var array = data.id; if that is more convenient for you.

Remember that data in your callback is just whatever you got as JSON. It can be an object (which is an associative array), an array, a string, a number, or a true, false or null value. If it is an object you access it using data.key, if it is an array you access it using data[index]. I say it because I suspect that you might be confusing arrays with objects here.

Upvotes: 2

Related Questions