简体   繁体   English

Javascript-在多维数组中查找多个值

[英]Javascript - Find multiple values in multidimensional array

Got some JSON that I need to go through to fetch some IDs. 有一些JSON,我需要通过获取一些标识。 The JSON looks like this: JSON如下所示:

var carousel = [
  {
    "acf": {
      "content": [
        {
          "acf_fc_layout": "custom",
          "content": "Some text"
        },
        {
          "acf_fc_layout": "exhibition",
          "exhibition": 2594
        },
        {
          "acf_fc_layout": "exhibition",
          "exhibition": 1234
        }
      ]
    },
  }
]

For every content where acf_fc_layout == exhibition I must fetch the value (ID) of exhibition so it can be used to fetch further data. 对于acf_fc_layout == exhibition每个content ,我必须获取acf_fc_layout == exhibition的值(ID),以便可以将其用于获取其他数据。 As you can see there's multiple exhibition IDs aswell. 正如你可以看到有多个展览的ID以及。

My confusion here is that there are both object and array, and that they're nested. 在这里,我的困惑是,有两个对象和数组,以及嵌套的时候。 I've done some similar stuff with jQuery, but that's out of the question this time. 我已经做了jQuery的一些类似的东西,但这不在这个时间的问题。 Don't think I need IE8 support, but still find this tricky.. 不要以为我需要IE8的支持,但仍觉得这个棘手..

carousel[0].acf.content.forEach(function (item) {
  if (item["acf_fc_layout"] === "exhibition") {
    // do some stuff
    // id for exhibition placed in item["exhibition"] 
  }
});

If your JSON simply looks as you say, this is a simple solution: 如果您的JSON看起来像您所说的那样,那么这是一个简单的解决方案:

var i;
for (i = 0; i < carousel[0].acf.content.length; i++) {
    if (carousel[0].acf.content[i].acf_fc_layout === "exhibition") {
        // do something with carousel[0].acf.content[i].exhibition
    }
}

Alternatively if you have much more content in your JSON, this might be relevant: 另外,如果您的JSON中包含更多内容,则可能与此有关:

var i, j;
for (i = 0; i < carousel.length; i++) {
    if (typeof carousel[i].acf != 'undefined' && typeof carousel[i].acf.content != 'undefined') {
        for (j = 0; j < carousel[i].acf.content.length; j++) {
            if (carousel[i].acf.content[j].acf_fc_layout === "exhibition") {
                // do something with carousel[i].acf.content[j].exhibition
            }
        }
    }
}

http://jsfiddle.net/oucp3v5x/ http://jsfiddle.net/oucp3v5x/

$(carousel).each(function(i, el){

    $(el.acf.content).each(function(i, el){
        if(el.acf_fc_layout === 'exhibition') {
            $('<div>', {
                text: el.exhibition
            }).appendTo($('#results'));
        }
    });

});

If acf have many content , you need to run additional loop and acf have to be array of objects. 如果acf有很多content ,则需要运行附加循环,并且acf必须是对象数组。

With your current structure, you need to use a foreach and check the value. 使用当前结构,您需要使用foreach并检查该值。

 var carousel = [
  {
    "acf": {
      "content": [
        {
          "acf_fc_layout": "custom",
          "content": "Some text"
        },
        {
          "acf_fc_layout": "exhibition",
          "exhibition": 2594
        },
        {
          "acf_fc_layout": "exhibition",
          "exhibition": 1234
        }
      ]
    },
  }
];

$.each(carousel[0].acf.content,function (i,v){
    if(v.acf_fc_layout == "exhibition")
        $(".result").append(v.exhibition+"<br>");
    });

JSFIddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM