在JSON对象上使用jQuery的find()与brnwdrng的问题类似,我正在寻找一种搜索类似JSON的对象的方法。假设我的对象的结构是这样的:TestObj = {
"Categories": [{
"Products": [{
"id": "a01",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a02",
"name": "Birch",
"description": "Short description of birch."
},
{
"id": "a03",
"name": "Poplar",
"description": "Short description of poplar."
}],
"id": "A",
"title": "Cheap",
"description": "Short description of category A."
},
{
"Product": [{
"id": "b01",
"name": "Maple",
"description": "Short description of maple."
},
{
"id": "b02",
"name": "Oak",
"description": "Short description of oak."
},
{
"id": "b03",
"name": "Bamboo",
"description": "Short description of bamboo."
}],
"id": "B",
"title": "Moderate",
"description": "Short description of category B."
}]};我想得到一个id =“A”的对象。我尝试过各种各样的东西,比如:$(TestObj.find(":id='A'"))但似乎没什么用。任何人都可以想到一种基于某些标准检索项目而不使用“每个”的方法吗?
3 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
这适用于[{“id”:“data”},{“id”:“data”}]
function getObjects(obj, key, val) { var newObj = false; $.each(obj, function() { var testObject = this; $.each(testObject, function(k,v) { //alert(k); if(val == v && k == key) { newObj = testObject; } }); }); return newObj;}
添加回答
举报
0/150
提交
取消