我已经在线查看,但我无法就我遇到的问题找到详细的答案。我有一个看起来像这样的对象:
{
"00001": {
"sid": "00001",
"oid": "00001",
"name": "operation 0001 service 1",
"description": "test operation 000001",
"returntype": "something",
"parameters": {
"00001": {
"pid": "00001",
"name": "parameter 00001 operation 0001 service 1",
"description": "test parameter 000001",
"type": "something"
}
}
},
"00002": {
"sid": "00002",
"oid": "00002",
"name": "operation 0001 service 2",
"description": "test operation 000001",
"returntype": "something",
"parameters": {}
},
"00003": {
"sid": "00003",
"oid": "00003",
"name": "operation 0001 service 3",
"description": "test operation 000001",
"returntype": "something",
"parameters": {}
},
"00004": {
"sid": "00004",
"oid": "00004",
"name": "operation 0001 service 4",
"description": "test operation 000001",
"returntype": "something",
"parameters": {}
},
"00005": {
"sid": "00005",
"oid": "00005",
"name": "operation 0001 service 5",
"description": "test operation 000001",
"returntype": "something",
"parameters": {}
},
"00006": {
"sid": "00001",
"oid": "00006",
"name": "operation 0001 service 6",
"description": "test operation 000001",
"returntype": "something",
"parameters": {}
}
}
我正在尝试遍历对象并能够返回具有特定sid的那些(即00001)我在javascript中知道obj中有一个for var,但我不确定如何按顺序实现它获得所需的输出。任何帮助或指导将不胜感激。
答案 0 :(得分:1)
如果你知道这只是一级深度(“sid”匹配只能在对象的第一级找到,而你也没有搜索嵌套对象),那么它更直接:
function findMatches(data, prop, val) {
var item, matches = [];
for (var obj in data) {
item = data[obj];
if (item[prop] === val) {
matches.push(item);
}
}
return matches;
}
var result = findMatches(myData, "sid", "00001");
答案 1 :(得分:0)
递归是要走的路。见@ T.J。克劳德的回答 Loop through nested objects with jQuery