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

时间:2022-02-25 01:58:37

I am totally new to vue js and I just want loop through the json data and want to find that if my required data is available in the file. this is the sample of 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"
    }
}]

I just want to loop through and find if "instance" contains the value twitteruser.

我只想循环遍历并查找“实例”是否包含值twitteruser。

I tried this code:

我试着这段代码:

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

3 个解决方案

#1


0  

you can iterate with following way ..

你可以用下面的方法来迭代。

    <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>

#2


1  

If your data is an array parsed from your JSON you can do this:

如果您的数据是从JSON解析的数组,您可以这样做:

var filtered = data.filter(function(item) {
   return item.instance && item.instance.twitteruser  
})

filtered will be a new array with only those elements that have a twitteruser

过滤将是一个新的数组,只有那些有twitteruser的元素

If you just want to know whether is exists, you can test for the length of filtered

如果只想知道是否存在,可以测试过滤的长度

if (filtered.length > 0) {
   // do something
}

#3


0  

If I understand you correctly, you have an array of objects, each of which contains another object under the instance key, and you want to find out if any of those instance objects contain the key twitteruser. Just write a for loop and check:

如果我理解正确的话,您有一个对象数组,每个对象都包含实例键下的另一个对象,您想要找出这些实例对象中是否有一个包含键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;
  }
}

#1


0  

you can iterate with following way ..

你可以用下面的方法来迭代。

    <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>

#2


1  

If your data is an array parsed from your JSON you can do this:

如果您的数据是从JSON解析的数组,您可以这样做:

var filtered = data.filter(function(item) {
   return item.instance && item.instance.twitteruser  
})

filtered will be a new array with only those elements that have a twitteruser

过滤将是一个新的数组,只有那些有twitteruser的元素

If you just want to know whether is exists, you can test for the length of filtered

如果只想知道是否存在,可以测试过滤的长度

if (filtered.length > 0) {
   // do something
}

#3


0  

If I understand you correctly, you have an array of objects, each of which contains another object under the instance key, and you want to find out if any of those instance objects contain the key twitteruser. Just write a for loop and check:

如果我理解正确的话,您有一个对象数组,每个对象都包含实例键下的另一个对象,您想要找出这些实例对象中是否有一个包含键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;
  }
}