Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.


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.

  • Get all the values
  • Isolate the id values of JSON objects
  • Loop another array
  • Check if json id is inside the other array
  • Access other json variables by id value

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

share|improve this question
I don't understand your problem. Have you tried anything? Of course you can store the response wherever you want and you can access any variable in upper scopes. You just have to make that the structure of your arrays are correct (e.g. idsArray = new Array(data.id) does not look right). – Felix Kling Mar 11 '11 at 13:46
I'm just looking for a minimal code approach to this. I'm haven't used jquery much and not familiar with all the snippets. Just trying to avoid looping everything and adding/checking values – ZolaKt Mar 11 '11 at 13:48
2  
@ZolaKt: Then what does the array in someOtherArray look like and what is the value of data? If we don't know the structure, we cannot tell you how to access it. And this is not related to jQuery at all, just basic JavaScript. – Felix Kling Mar 11 '11 at 13:49
2  
@ZolaKt: But what is data? Is it a single object or is it an array of objects? Why not just post the structure, makes it much easier. – Felix Kling Mar 11 '11 at 13:53
1  
@ZolaKt: Show some example JSON, and some example contents of someOtherArray or whatever else you want to use, and tell us what exactly do you want to do with it. It is not clear what do you want to do and with what. – rsp Mar 11 '11 at 14:04
show 2 more comments

2 Answers

up vote 3 down vote accepted

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
share|improve this answer
thanks. yes i know someOtherArray is smaller – ZolaKt Mar 11 '11 at 14:14
btw. would jQuery.parseJSON take care of the mapping? – ZolaKt Mar 11 '11 at 14:15
@ZolaKt: No. If your JSON is [{...}, {...}] then you will get a JavaScript array, no matter what. You have to create your JSON as {id1: {...}, id2: {...}} and then you will get a JavaScript object (which would be the map). jQuery.parseJSON is just parsing the JSON to pass into JavaScript data structures and this is already done by using getJSON. – Felix Kling Mar 11 '11 at 14:19
Any advice on how to adjust the server return? In php, I'm returning/echoing an array of custom objects with json_encode($items) – ZolaKt Mar 11 '11 at 14:22
@ZolaKt: Please see my update. – Felix Kling Mar 11 '11 at 14:25
show 2 more comments

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.

share|improve this answer
ok, nice. any advice how to get just to id keys without looping everything with $.each key-value – ZolaKt Mar 11 '11 at 13:50
@ZolaKt: It would be much easier if you showed some example JSON data that you need to traverse. – rsp Mar 11 '11 at 13:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.