Search code examples
phpajaxresponse

Loop AJAX Response from php


I want to loop ajax response

response = [
             ["u.profile"],
             ["r.useractivity"],
             ["i.items_job"],
             ["i.setup"],
             ["search"],
             ["i.items_assortment"]
]

I want data = u.profile; data = r.useractivity; etc

Tried Method :

$.each(response,function(key,value){
    console.log(key+":"+value);
});

Getting error in console

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [["u.profile"],["r.useractivity"],["i.items_job"],["i.setup"],["search"],["i.items_assortment"]]


Solution

  • The problem is that your response var is an array of arrays (an object) and the default keys are integers.
    It would be an easier (and better) way if you could change it into an array of strings like this one :

    var response = ["u.profile",
                   "r.useractivity",
                   "i.items_job",
                   "i.setup",
                   "search",
                   "i.items_assortment"];
    

    With this, you can easily loop your response like this :

    for(var info in response)
        console.log(info+':'+response[info]);
    

    Hope this will help !