如何在Javascipt中迭代嵌套的对象数组?我有一个名为obj
的对象。我想要检索in
是credit
和out
是bank
的对象。
// I have tried using filter but returns empty array
const s = obj.filter(function(t){
return t.in == "credit" && t.out == "bank";
})
console.log(s);
这是数据:
var obj = [{
"btob": [{
"id": "trans",
"in": "bank",
"out": "bank",
"value": 10
}],
"ctob": [{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
}],
"dtob": [{
"id": "trans",
"in": "debit",
"out": "bank",
"value": 30
}]
}, {
"btob": [{
"id": "fund",
"in": "bank",
"out": "bank",
"value": 10
}],
"ctob": [{
"id": "fund",
"in": "credit",
"out": "bank",
"value": 10
}],
"dtob": [{
"id": "fund",
"in": "debit",
"out": "bank",
"value": 30
}]
}]
预期产出:
[{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
},
{
"id": "fund",
"in": "credit",
"out": "bank",
"value": 10
}]
这是一个功能风格的解决方案:
data.flatMap(obj => Object.values(obj).flatMap(arr =>
arr.filter(t => t.in === "credit" && t.out === "bank")
));
const data = [{"btob": [{"id": "trans","in": "bank","out": "bank","value": 10}],"ctob": [{"id": "trans","in": "credit","out": "bank","value": 20}],"dtob": [{"id": "trans","in": "debit","out": "bank","value": 30}]}, {"btob": [{"id": "fund","in": "bank","out": "bank","value": 10}],"ctob": [{"id": "fund","in": "credit","out": "bank","value": 10}],"dtob": [{"id": "fund","in": "debit","out": "bank","value": 30}]}];
const result = data.flatMap(obj => Object.values(obj).flatMap(arr => arr.filter(t => t.in === "credit" && t.out === "bank")));
console.log(result);
但就像被评论一样,如果你的对象密钥“ctob”意味着“贷记到银行”,那么就不需要测试嵌套的“信用”和“银行”属性值。
由于密钥ctob
引用的对象符合您的选择要求,您可以简单地执行此操作:
const output = obj.map(entry => {
return entry.ctob[0];
});
var obj = [{
"btob": [{
"id": "trans",
"in": "bank",
"out": "bank",
"value": 10
}],
"ctob": [{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
}],
"dtob": [{
"id": "trans",
"in": "debit",
"out": "bank",
"value": 30
}]
}, {
"btob": [{
"id": "fund",
"in": "bank",
"out": "bank",
"value": 10
}],
"ctob": [{
"id": "fund",
"in": "credit",
"out": "bank",
"value": 10
}],
"dtob": [{
"id": "fund",
"in": "debit",
"out": "bank",
"value": 30
}]
}];
const output = obj.map(entry => {
return entry.ctob[0];
});
console.log(output);
当然,如果您想要绝对确定,则必须遍历每个嵌套对象。请记住,由于每个嵌套数组的长度为1(它是一个单长度的对象数组),因此在比较其键之前,需要使用[0]
来访问正确的对象:
const output = [];
obj.forEach(entry => {
Object.keys(entry).forEach(key => {
const entity = entry[key][0];
if (entity.in === 'credit' && entity.out === 'bank') {
output.push(entity);
}
});
});
var obj = [{
"btob": [{
"id": "trans",
"in": "bank",
"out": "bank",
"value": 10
}],
"ctob": [{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
}],
"dtob": [{
"id": "trans",
"in": "debit",
"out": "bank",
"value": 30
}]
}, {
"btob": [{
"id": "fund",
"in": "bank",
"out": "bank",
"value": 10
}],
"ctob": [{
"id": "fund",
"in": "credit",
"out": "bank",
"value": 10
}],
"dtob": [{
"id": "fund",
"in": "debit",
"out": "bank",
"value": 30
}]
}];
const output = [];
obj.forEach(entry => {
Object.keys(entry).forEach(key => {
const entity = entry[key][0];
if (entity.in === 'credit' && entity.out === 'bank') {
output.push(entity);
}
});
});
console.log(output);
你必须遍历数组和单个数组项的每个属性;我以更易读的方式编写代码,添加一些注释:
var searched = [];
// iterate on each array elements
for(var i = 0; i < obj.length; i++){
// take the array element as an object
var element = obj[i];
// iterate to all the properties of that object
for (var property in element) {
if (element.hasOwnProperty(property)) {
// take the property as an object
var propObj = element[property][0];
// verify if the property has searched value, if so, add to the result array
if(propObj.in == "credit" && propObj.out == "bank")
searched.push(propObj)
}
}
}
// print searched array
console.log(searched);
这是你的解决方案:
x=[];
obj.forEach((t)=>{
for(key in t){
if ((t[key][0].in == "credit") && (t[key][0].out == "bank")){
x.push(t[key][0])
}
}
});
console.log(x);