简体   繁体   中英

How to iterate over a nested object array in Javascript

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
  }]

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 " c redit to b ank", then there should be no need to test for nested "credit" and "bank" property values.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM