如何使用jQuery搜索嵌套的JSON数组?

时间:2019-06-06 09:42:47

标签: javascript jquery json

我正在尝试遍历JSON文件(下面粘贴的示例对象)中的“机器”对象数组。每个对象都包含一个嵌套的安装的软件数组,我希望能够通过输入字段动态搜索整个软件。也就是说,确定用户输入字符串(例如“虚幻”)与安装了名称为“虚幻”的软件的计算机之间的匹配项。

我已经创建了下面的算法来搜索每个JSON对象顶层的其他对象,我认为这将是类似的东西,但是到目前为止,反复试验对我的服务还不够好!

仅添加value.software.product_name来检索每个软件名称是行不通的,因为该值在对象中更深。

$.getJSON('../assets/mbid_directory.json', function(data){
        $.each(data, function(key, value){
          if(value.general.building_name.search(expression) != -1 || value.general.room_name.search(expression) != -1 || value.hardware.cpu.search(expression) != -1 ||
            value.hardware.memory.search(expression) != -1 || value.hardware.gpu.search(expression) != -1 || value.hardware.screen_resolution.search(expression) != -1 ||
              value.hardware.notable_peripherals.search(expression) != -1) {
                //do something
          }
        })
      })
  {
    "general": {
      "machine_id": 2,
      "machine_ip_address": "192.168.0.18",
      "building_name": "Mellor",
      "room_name": "S509",
      "map_location": {
        "lat": "-25.363",
        "lng": "131.044"
      }
    },
    "hardware": {
      "cpu": "AMD Ryzen 5 3570K 3.40GHz",
      "memory": "8GB DDR3 2600Ghz",
      "gpu": "Nvidia GTX 790 Ti 3GB",
      "screen_resolution": "1920x1200",
      "notable_peripherals": "Dual-monitors"
    },
    "software": [
      {
        "product_title": "Windows 10 Education",
        "product_version": "6.2.9200.16384"
      },
      {
        "product_title": "Unreal Engine Games Studio",
        "product_version": "8.4.2"
      },
      {
        "product_title": "Microsoft Visual Studio 2017",
        "product_version": "5.5.106.3"
      }
    ]
  },

任何指导将不胜感激!

2 个答案:

答案 0 :(得分:1)

您可以在软件阵列上使用filter,例如:

$.getJSON('../assets/mbid_directory.json', function(data){
    $.each(data, function(key, value){
        if (value.general.building_name.search(expression) != -1 ||
            value.general.room_name.search(expression) != -1 ||
            value.hardware.cpu.search(expression) != -1 ||
            value.hardware.memory.search(expression) != -1 ||
            value.hardware.gpu.search(expression) != -1 ||
            value.hardware.screen_resolution.search(expression) != -1 ||
            value.hardware.notable_peripherals.search(expression) != -1 ||
            value.software.filter(function(elem){
              return elem.product_title.search(expression) != -1 ||
                     elem.product_version.search(expression) != -1
              }).length > 0) {
              console.log("Match!");
        }
    });
});

答案 1 :(得分:0)

您可以使用JavaScript array of objectfilter这样的方法从includes搜索文本,可以按照以下说明进行简化。

let machineObj = JSON.parse(`{
  "general": {
    "machine_id": 2,
    "machine_ip_address": "192.168.0.18",
    "building_name": "Mellor",
    "room_name": "S509",
    "map_location": {
      "lat": "-25.363",
      "lng": "131.044"
    }
  },
  "hardware": {
    "cpu": "AMD Ryzen 5 3570K 3.40GHz",
    "memory": "8GB DDR3 2600Ghz",
    "gpu": "Nvidia GTX 790 Ti 3GB",
    "screen_resolution": "1920x1200",
    "notable_peripherals": "Dual-monitors"
  },
  "software": [
    {
      "product_title": "Windows 10 Education",
      "product_version": "6.2.9200.16384"
    },
    {
      "product_title": "Unreal Engine Games Studio",
      "product_version": "8.4.2"
    },
    {
      "product_title": "Microsoft Visual Studio 2017",
      "product_version": "5.5.106.3"
    }
  ]
}`);

let textSearch = 'Unreal';
let result = (machineObj.software).filter(o => o.product_title.includes(textSearch));
console.log(result);