在给定的 属性 javascript 中迭代给定特定值的对象和过滤器
iterate through object and filter given a specific value in a given property javascript
我在网上查看过,但未能找到有关我所遇到问题的详细答案。我有一个看起来像这样的对象:
{
"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": {}
}
}
我正在尝试遍历对象并能够 return 具有特定 sid 的对象(即 00001)我知道在 javascript 中有一个 for var in obj,但是我不确定如何实现它以获得所需的输出。任何帮助或指导将不胜感激。
递归 是要走的路。参见@T.J。克劳德的回答
Loop through nested objects with jQuery
如果您知道这只是一层深度("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");
我在网上查看过,但未能找到有关我所遇到问题的详细答案。我有一个看起来像这样的对象:
{
"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": {}
}
}
我正在尝试遍历对象并能够 return 具有特定 sid 的对象(即 00001)我知道在 javascript 中有一个 for var in obj,但是我不确定如何实现它以获得所需的输出。任何帮助或指导将不胜感激。
递归 是要走的路。参见@T.J。克劳德的回答 Loop through nested objects with jQuery
如果您知道这只是一层深度("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");