0

I have an Object that contains an associative array

enter image description here

The value stored in the array is an Object

Within the each function, I want to access one of the values in the value object (responseText)

enter image description here

My code is as follows

 var apiNameArray = getDataSourceApiNames();
        var apiResults = {};
        var deferred;

        for (let i = 0; i < apiNameArray.length; i++) {
            var apiName = apiNameArray[i];
            console.log("apiName = " + apiName);
            deferred = $.ajax({
                    type: "GET",
                    url: api_URL + "memberdetails/" + memberNumber,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                }
            );
            apiResults[apiName] = deferred;
        }

        $.when.apply($, apiResults).then(function () {
            console.log(apiResults);
            $.each(apiResults, function (key, value) {
                console.log(key);
                console.log(value);
                console.log(value.responseText);
           });
        });

For some reason, value.responseText is returning undefined. How am I suppose to be accessing this value/property? I have tried value["responseText"], apiResults[key].responseText all with no success

6
  • Can you show the ajax request code too? It looks like the value you are after is out of scope. Commented Feb 26, 2018 at 10:23
  • use JSON.parse() method. Commented Feb 26, 2018 at 10:24
  • If the first screenshot is the OP of the console.log(apiResults); then you can simply access the responseText like apiResults[0]['responseText']. You can replace the 0 with a loop index. Commented Feb 26, 2018 at 10:28
  • Post amended to show AJAX call Commented Feb 26, 2018 at 10:29
  • @swetanshkumar - it is already an object Commented Feb 26, 2018 at 10:33

1 Answer 1

1

As the apiResults is a object you can loop though the keys and use apiResults[key].responseText to access the value.

$.when.apply($, apiResults).then(function () { $.each(apiResults, function (key, value) { console.log(apiResults[key].responseText); }); });

or

$.when.apply($, apiResults).then(function () { Object.keys('apiResults').forEach(function(item, index){ console.log(apiResults[item].responseText) }) });

Here Object.keys will return an array of all the keys in an object.

Sign up to request clarification or add additional context in comments.

5 Comments

That still returns undefined for apiResults[key].responseText
@mike can you please create a fiddle of your code. As per my understanding the above code should work.
How do I mimic the AJAX response in jsfiddle?
Save the response in a JS variable and run the loop.
I am going to undo the downvote and mark as the answer - it was because my promise was not waiting for the API success to complete before populating the array. So when I looped through the values, it was empty (and therefore undefined)

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.