1
我有一個JavaScript對象,類似於JSON數組,我需要查詢。我想知道是否有一種方法可以使用jQuery來查找節點值,而無需遍歷整個對象並按順序測試每個節點的匹配。例如,這裏是對象:用jQuery搜索JSON對象
Test = {};
Test.Species = {
"Category": [{
"Product": [{
"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."
},
{
"Product": [{
"id": "c01",
"name": "Ebony",
"description": "Short description of ebony."
},
{
"id": "c02",
"name": "Rosewood",
"description": "Short description of rosewood."
},
{
"id": "c03",
"name": "Bubinga",
"description": "Short description of bubinga."
}],
"id": "C",
"title": "Expensive",
"description": "Short description of category C."
}]
};
我怎麼能檢索產品的名稱和說明,如果給出的ID(再次,沒有一個嵌套的每()通過整個事情)?我嘗試過jQuery的find()和屬性選擇器([id = a03]),但沒有成功......我唯一可以精確定位節點的方法是使用索引位置(例如
var x = $(Test.Species.Category[2].Product[1].attr('description'));
),但這不是目標。
我在尋找這些方針的東西多,但是這似乎並沒有工作:
var x = $(Test.Species.Category[id='B'].Product[id='b02'].attr('description'));
JavaScript有一個功能的過濾器(),這可能與幫助,但它確實不能在IE7或更低版本中工作。 – Pieter 2010-12-11 02:19:11