How can I iterate through a nested array of objects in Javascipt? I have an object named obj. I want to retrieve the object where in is credit and out is 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);
This is the data:
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
}]
}]
Expected Output:
[{
"id": "trans",
"in": "credit",
"out": "bank",
"value": 20
},
{
"id": "fund",
"in": "credit",
"out": "bank",
"value": 10
}]
tas the inner array, thoughtis an object containing a dynamic key (ctob, btob) and, for that key, an array of objects. You likely want to filter on that array instead.