如何通过循环JSON查找所需的数据

时间:2017-07-27 19:38:04

标签: javascript json

我是vue js的新手,我只想循环浏览json数据,并希望找到我所需的数据在文件中可用。 这是json的样本:

[
{
    "id": "text-5",
    "widget": "hello",
    "params": {
        "0": "section-right",
        "name": "Right",
        "id": "section",
        "description": "",
        "class": "",
        "after_title": "",
        "widget_id": "text-5"
    },
    "instance": {
        "title": "St",
        "text": "",
        "filter": false,
        "sidebar": "se-right",
        "sidebar-order": 0,
        "inherited": "yes",
        "number": 0
    }
},
{
    "id": "text-5",
    "widget": "hello",
    "params": {
        "0": "section-right",
        "name": "Right",
        "id": "section",
        "description": "",
        "class": "",
        "after_title": "",
        "widget_id": "text-5"
    },
    "instance": {
        "title": "Twitter Feed",
        "twitteruser": "Stei",
        "sidebar": "sectht",
        "sidebar-order": "4"
    }
}]

我只想循环查找“instance”是否包含值twitteruser。

我试过这段代码:

const data = JSON.parse(res).data
console.log(data[0].instance)

3 个答案:

答案 0 :(得分:1)

如果var filtered = data.filter(function(item) { return item.instance && item.instance.twitteruser }) 是从JSON解析的数组,则可以执行以下操作:

filtered

twitteruser将是一个新数组,只包含具有filtered

的元素

如果您只想知道是否存在,可以测试if (filtered.length > 0) { // do something }

的长度
{{1}}

答案 1 :(得分:0)

如果我理解正确,你有一个对象数组,每个对象都包含instance键下的另一个对象,你想知道这些instance对象中是否有任何对象包含密钥twitteruser。只需编写一个for循环并检查:



const data = [{
    "id": "text-5",
    "widget": "hello",
    "params": {
      "0": "section-right",
      "name": "Right",
      "id": "section",
      "description": "",
      "class": "",
      "after_title": "",
      "widget_id": "text-5"
    },
    "instance": {
      "title": "St",
      "text": "",
      "filter": false,
      "sidebar": "se-right",
      "sidebar-order": 0,
      "inherited": "yes",
      "number": 0
    }
  },
  {
    "id": "text-5",
    "widget": "hello",
    "params": {
      "0": "section-right",
      "name": "Right",
      "id": "section",
      "description": "",
      "class": "",
      "after_title": "",
      "widget_id": "text-5"
    },
    "instance": {
      "title": "Twitter Feed",
      "twitteruser": "Stei",
      "sidebar": "sectht",
      "sidebar-order": "4"
    }
  }
];

for (var i = 0; i < data.length; i++) {
  if (data[i].instance.twitteruser !== undefined) {
    console.log("Found it at index " + i + ".");
    break;
  }
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以按照以下方式进行迭代..

    <script>
    var json_data = [
    {
        "id": "text-5",
        "widget": "hello",
        "params": {
            "0": "section-right",
            "name": "Right",
            "id": "section",
            "description": "",
            "class": "",
            "after_title": "",
            "widget_id": "text-5"
        },
        "instance": {
            "title": "St",
            "text": "",
            "filter": false,
            "sidebar": "se-right",
            "sidebar-order": 0,
            "inherited": "yes",
            "number": 0
        }
    },
    {
        "id": "text-5",
        "widget": "hello",
        "params": {
            "0": "section-right",
            "name": "Right",
            "id": "section",
            "description": "",
            "class": "",
            "after_title": "",
            "widget_id": "text-5"
        },
        "instance": {
            "title": "Twitter Feed",
            "twitteruser": "Stei",
            "sidebar": "sectht",
            "sidebar-order": "4"
        }
    }];


    for( var i = 0; i < json_data.length; i++) {

        if(typeof json_data[i].instance == 'object' && typeof json_data[i].instance.twitteruser != 'undefined'){
            console.log(json_data[i].instance.twitteruser);
        }

    }

    </script>