5


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.

7
  • 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). Commented Mar 11, 2011 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 Commented Mar 11, 2011 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. Commented Mar 11, 2011 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. Commented Mar 11, 2011 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. Commented Mar 11, 2011 at 14:04

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

btw. would jQuery.parseJSON take care of the mapping?
@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.
Any advice on how to adjust the server return? In php, I'm returning/echoing an array of custom objects with json_encode($items)
hmmm, not that simple. its a whole oop structure based on objects, not associative arrays, so i think ill just keep the extra loop and go with your first example. thanks
@ZolaKt: You're welcome. For completeness: You can also iterate over the $items array before you pass it json_encode and create a new array with this structure. I just wanted to avoid looping over the data twice...
2

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.

2 Comments

ok, nice. any advice how to get just to id keys without looping everything with $.each key-value
@ZolaKt: It would be much easier if you showed some example JSON data that you need to traverse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.