访问特定的阵列节点bu名称

时间:2019-01-28 10:25:09

标签: jquery arrays

我想从以下数组节点访问“ Celebrity”节点,该节点本身位于第一个数组层中,因此可通过以下方式访问:

  $.each( data, function( key, val ) {


      var celItem = data[0];
      console.log($(celItem));

    });

enter image description here

Above是Chrom控制台日志。.我的思想崩溃了-谁能给我快速的指导?

2 个答案:

答案 0 :(得分:1)

由于data是具有一个对象元素的数组,因此不需要循环。只需访问所需的属性即可:

data[0].Celebrities

由于这本身就是数组,因此可以对其进行迭代:

data[0].Celebrities.forEach(function(celebrity) { 
    console.log(celebrity);
});

答案 1 :(得分:0)

您可以简单地检查当前循环键是否为Celebrities,然后使用其值初始化变量。

var str = '{"VenueName": "SuperTheatre", "Celebrities": [1,2,3], "Schedules": [4,5,6]}';
var data = JSON.parse(str);
$.each(data, function(key, val) {

  if (key == "Celebrities") {
    var celItem = val;
    console.log(celItem);
  }


});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>