我有以下JSON数据:
{
"VMs":[
{
"ID":"VM-WIN7-64",
"OS":"Windows 7",
"FLAVOUR":"VM-IE8-001-preq",
"ADAPTER":"Win 9",
"Paths":"D:\\VirtualMachines\\Win7_X64_VM-001\\Win7_X64_VM-001.vmx"
},
{
"ID":"VM-WIN7-6",
"OS":"Windows jj7",
"FLAVOUR":"VM-IE8-001-preq",
"ADAPTER":"Winjjjjj 9",
"Paths":"f:\\VirtualMachines\\Win7_X64_VM-001\\Win7_X64_VM-001.vmx"
}
]
}
在这个JSON中,我得到了值"ID":"VM-WIN7-64"
。使用此ID,如何从此JSON数据中过滤相应的适配器名称"ADAPTER":"Win 9"
?
答案 0 :(得分:1)
使用Array.prototype.filter
方法。
var filtered = data.VMs.filter(function (element) {
return element.ID == "VM-WIN7-64";
});
(data
变量包含整个JSON数据“VMs”数组)
然后,filtered[0].ADAPTER
的值为“Win 9”