gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:31:32 24 4
gpt4 key购买 nike

我正在尝试迭代 JSON 文件中的“机器”对象数组(下面粘贴的示例对象)。每个对象都包含一个已安装软件的嵌套数组,我希望能够通过输入字段动态搜索这些软件。即识别用户输入字符串(例如“Unreal”)与安装了名称包含“Unreal”的软件的计算机之间的匹配。

我已经创建了下面的算法来搜索每个 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"
}
]
},

如有任何指导,我们将不胜感激!

最佳答案

您可以在软件阵列上使用过滤器,例如:

$.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!");
}
});
});

关于javascript - 如何使用 jQuery 搜索嵌套的 JSON 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56474912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com