tags:

views:

31

answers:

1

I have this json

{"suggestions":["M.I.A.","M.","Mindless Self Indulgence","The Cure","Telefon Tel Aviv","M","J. Ralph","Jason Mraz","Carbon Based Lifeforms","Cycle of Pain","Chantal Kreviazuk","-M-","ayumi hamasaki","R.E.M.","Donny McCaslin","Penfold","HEALTH","R. Kelly","DJ Khaled","Eminem","Spose","T.I.","The Lonely Island","H.I.M. (His Infernal Majesty)","Dropkick Murphys","Taylor Swift"],"query":"m"}

which i am getting from this ajax call

            $.getJSON('<%= ajax_path("artistName") %>', req, function(data) {

                //create array for response objects
                var suggestions = [];
                console.log(data);
                //process response
                $.each(data, function(i, val){                                
                    suggestions.push(val.name);
                });
                console.log(suggestions);

                //pass array to callback
                add(suggestions);
            });
        },

why is my suggestions still null

+2  A: 

You don't have to loop through, suggestions is already an array available for use, so replace this:

var suggestions = [];
console.log(data);
//process response
$.each(data, function(i, val){                                
    suggestions.push(val.name);
});
console.log(suggestions);

With this:

var suggestions = data.suggestions;
console.log(suggestions);

Then your array being passed to add() will have the results from your JSON response.

Nick Craver