Javascript AJAX:如何在多维数组上获取数组

Javascript AJAX:如何在多维数组上获取数组,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个json,它是。我只想得到具体的数据 obj['contacts']['name'] 我怎样才能得到它 obj['contacts']['name'] 联系人列表中的姓名 这是我的代码: $.ajax({ type: 'GET', dataType: 'json', url: uri, cache: false, contentType: 'application/json', success: function(data) { for (var obj

我有一个json,它是。我只想得到具体的数据

obj['contacts']['name']

我怎样才能得到它

obj['contacts']['name']

联系人列表中的姓名

这是我的代码:

$.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']);
    }
  }
});

只需枚举返回的对象“contacts”属性:

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

在您的情况下,这就是您希望从
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);
  }
});
obj
是关键。迭代
data.contacts
并从中获取
name
obj.contacts.map(函数(项){return item.name})可能重复的