gpt4 book ai didi

javascript - 如何获取对象的匹配对象数组和json对象

转载 作者:行者123 更新时间:2023-11-30 00:03:56 24 4
gpt4 key购买 nike

这是我的对象。我只需要访问“oname”:“Fon”和“bid”:1个对象的详细信息。无需访问 "bid": "2"对象详细信息

{
"oid": "1",
"oname": "Fon",
"bun": [{
"bid": "1",
"bname": "Ets",
"dep": [{
"did": "1",
"dname": "Dptment",
"pids": [{
"pid": "1",
"st": "active"
}, {
"pid": "2",
"st": "active"
}]
}]
}, {
"bid": "2",
"bname": "US",
"description": "unit2",
"dep": []
}]
}

最佳答案

您可以使用filter 方法来获取所需的数据

 var arr = {
"oid": "1",
"oname": "Fon",
"bun": [{
"bid": "1",
"bname": "Ets",
"dep": [{
"did": "1",
"dname": "Dptment",
"pids": [{
"pid": "1",
"st": "active"
}, {
"pid": "2",
"st": "active"
}]
}]
}, {
"bid": "2",
"bname": "US",
"description": "unit2",
"dep": []
}]
}

var oname = arr.oname;
var data = arr.bun.filter(function (a) { return a.bid == "1" });

关于javascript - 如何获取对象的匹配对象数组和json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39227507/

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