ar em
ar em

Reputation: 444

AJAX: How to get array on multi dimensional array

I have a json which is . I just want to get specific data which is

obj['contacts']['name']

How can i get

obj['contacts']['name']

name on Contacts array

enter image description here

This is my code:

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: uri,
  cache: false,
  contentType: 'application/json',
  success: function(data) {
    for (var obj in data) {
      console.log(obj['contacts']['name']);
    }
  }
});

Upvotes: 2

Views: 497

Answers (2)

Parvez Rahaman
Parvez Rahaman

Reputation: 4387

In your case this is how you want get name from contacts

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: uri,
  cache: false,
  contentType: 'application/json',
  success: function(data) {
    if (!data.contacts) return;
    var names = data.contacts.map(function(dt) {
      return dt.name;
    });
    console.log(names);
  }
});

Upvotes: 1

wayofthefuture
wayofthefuture

Reputation: 9395

Just enumerate the returned object "contacts" property:

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: uri,
  cache: false,
  contentType: 'application/json',
  success: function(data) {
    data.contacts.forEach(function(contact) {
      console.log(contact.name);
    });
  }
});

Upvotes: 1

Related Questions