如何访问嵌套对象数组Javascript

时间:2017-03-30 21:08:10

标签: javascript arrays nested

我知道以前曾经问过,但我似乎无法找到答案。 我只想知道如何访问service_name array

console.log中的值

下面是数组,只是从数据中删除。

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]


console.log(JSON.stringify(a, 0, 4))

5 个答案:

答案 0 :(得分:1)

你可以像数组一样访问它,这就是你如何访问数组中第4项的service_name值

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]


console.log(JSON.stringify(a[3].service_name[0]))
console.log(JSON.stringify(a[3].service_name[1]))

答案 1 :(得分:0)

只是做:

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]
a.forEach((e)=>console.log(e.service_name));

如果您需要结果数组:

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]
var res = a.map((e)=>e.service_name);
console.log(res);

答案 2 :(得分:0)

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]

a.forEach(function(element) {
  console.log(element.service_name);
});

答案 3 :(得分:0)

您可以创建一个函数来返回其ID与您要查找的ID匹配的元素的服务名称数组,如下所示:



function getServiceNamesById (data, id) {
  for (var i = 0; i < data.length; i++) {
    if (data[i].ID === id) {
      return data[i].service_name;
    }
  }
  return [];
}

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}];

var id = 226619;

console.log(getServiceNamesById(a, id));
&#13;
&#13;
&#13;

答案 4 :(得分:0)

试试这个:

  var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]

 a.forEach(function(item){ // item is an object
     item.service_name.forEach(function(service){ //item.service_name is  an array
     console.log(service);
     });
 });
祝你好运