1

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
 }]
asked Apr 3, 2019 at 8:33
7
  • 4
    as per you current expected output, you cant same same key in object Commented Apr 3, 2019 at 8:36
  • The output is invalid, since you can't have the same key in the same object. Perhaps you wanted an array of objects instead? Besides, you are considering t as the inner array, though t is 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. Commented Apr 3, 2019 at 8:37
  • 1
    Also your object has missing "," at the end of some lines. Commented Apr 3, 2019 at 8:38
  • @briosheje yes, updated the output Commented Apr 3, 2019 at 8:40
  • @manuman94 apologies and updated changes Commented Apr 3, 2019 at 8:40

4 Answers 4

2

Here is a functional style solution:

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);

But like was commented, if your object key "ctob" means "credit to bank", then there should be no need to test for nested "credit" and "bank" property values.

answered Apr 3, 2019 at 8:54
Sign up to request clarification or add additional context in comments.

Comments

1

Since the object referenced by the key ctob matches your selection requirement, you can simply do this:

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);

Of course, if you want to be absolutely sure, you will have to go through each nested object. Remember that since each nested array has a length of one (it is a single-length array of objects), you need to use [0] to access the correct object before comparing its keys:

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);

answered Apr 3, 2019 at 8:51

Comments

1

You have to iterate trought the array and on each property of single array item; I write the code in a more readable way, adding some comments:

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);
answered Apr 3, 2019 at 9:00

Comments

0

Here is your solution:

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);
answered Apr 3, 2019 at 8:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.