forexknight forexknight - 5 months ago 8
jQuery Question

How to access json muliple data

I want to ask how can I access to my multiple data in jquery (json response).
Here's how my data looks like:
enter image description here

How can I display all kid's names?

$.ajax({
...
}).done(function(kids){
console.log(kids);
});

Answer

Assuming the call is:

$.ajax({
    ...
}).done(function(data){
    // code here
});

You can loop through if you want to work on one item at a time.

for (var i in data.kids) {
  console.log(data.kids[i].name)
}

If you want to extract all the names, you can use jquery .map, eg:

var names = $(data.kids).map(function(i,e) { return e.name }).get();
console.dir(names)

then you can show them all in one go:

console.log(names.join(", "))

Here's a fiddle, with console output: https://jsfiddle.net/cvre71dr

Comments