我想知道如何遍历Javascipt中嵌套的对象数组吗?我有一个名为obj的示例对象。我想根据条件'in'是'string'和'out''number'来检索对象。
// tried got stuck
const output = [];
myList.forEach(entry => {
Object.keys(entry).forEach(key => {
const entity = entry[key][0];
if (entity.in === "string" && entity.out === "number") {
output.push(entity);
}
});
});
var obj = [{
"ston": [{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
},{
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}],
"nton": [{
"id": "identity1",
"in": "number",
"out": "number",
"value": 20
},{
"id": "identity2",
"in": "number",
"out": "number",
"value": 30
}]
}]
预期产量
[{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
},{
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}]
答案 0 :(得分:1)
简单的递归函数:
var obj = [{
"ston": [{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
}, {
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}],
"nton": [{
"id": "identity1",
"in": "number",
"out": "number",
"value": 20
}, {
"id": "identity2",
"in": "number",
"out": "number",
"value": 30
}]
}];
function getObjects(inVal, outVal) {
var matches = [];
obj.forEach(child => {
Object.values(child).forEach(arr => {
if (arr.some(e => e.in == inVal && e.out == outVal)) {
matches.push([...arr.filter(e => e => e.in == inVal && e.out == outVal)]);
}
});
});
return matches.flat();
}
console.log(getObjects("string", "number"));
答案 1 :(得分:1)
在这里,您有一个解决方案,主要使用Array.reduce()遍历数组的外部对象,从每个外部对象获取值的展平数组,以创建具有内部对象的数组,然后过滤满足条件的对象条件,同时将它们保存在新数组中:
var obj = [
{
"ston": [
{"id": "identity1", "in": "string", "out": "number", "value": 10},
{"id": "identity2", "in": "string", "out": "number", "value": 10}
],
"nton": [
{"id": "identity1", "in": "number", "out": "number", "value": 20},
{"id": "identity2", "in": "number", "out": "number", "value": 30}
]
}
];
let res = obj.reduce((acc, o) =>
{
acc = acc.concat(Object.values(o).flat().filter(
o => o.in === "string" && o.out === "number"
));
return acc;
}, []);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
使用过的方法的其他文档:
或者,经过一段时间的思考,您可以将下一个简化版本与Array.map()
一起使用
var obj = [
{
"ston": [
{"id": "identity1", "in": "string", "out": "number", "value": 10},
{"id": "identity2", "in": "string", "out": "number", "value": 10}
],
"nton": [
{"id": "identity1", "in": "number", "out": "number", "value": 20},
{"id": "identity2", "in": "number", "out": "number", "value": 30}
]
}
];
let res = obj.map(Object.values).flat(2).filter(
o => o.in === "string" && o.out === "number"
);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
答案 2 :(得分:1)
您可以使用将该对象重建为嵌套数组,然后展平,最后select distinct /* I believe the sys.sys views were added in 2012 or so, still works in 2017 */
od.name caller_procedure_name
,o.name called_procedure_name
from sys.sysdepends d
inner join sys.sysobjects o on o.id = d.depid and o.type = 'P'
inner join sys.sysobjects od on od.id = d.id and od.type = 'P'
select distinct /* should work all the way back to sql 2000, still works in 2017 */
od.name caller_procedure_name
,o.name called_procedure_name
from dbo.sysdepends d
inner join dbo.sysobjects o on o.id = d.depid and o.type = 'P'
inner join dbo.sysobjects od on od.id = d.id and od.type = 'P'
。
filter