如何在Javascript中迭代嵌套对象数组

如何在Javascript中迭代嵌套对象数组,javascript,jquery,arrays,object,Javascript,Jquery,Arrays,Object,如何在Javascipt中迭代嵌套的对象数组?我有一个名为obj的对象。我想检索in是credit,out是bank的对象 以下是数据: var obj=[{ btob:[{ id:trans, 在:银行, 出:银行, 数值:10 }], ctob:[{ id:trans, 在:信贷, 出:银行, 价值:20 }], dtob:[{ id:trans, 在:借方, 出:银行, 价值:30 }] }, { btob:[{ id:基金, 在:银行, 出:银行, 数值:10 }], ctob:[{ i

如何在Javascipt中迭代嵌套的对象数组?我有一个名为obj的对象。我想检索in是credit,out是bank的对象

以下是数据:

var obj=[{ btob:[{ id:trans, 在:银行, 出:银行, 数值:10 }], ctob:[{ id:trans, 在:信贷, 出:银行, 价值:20 }], dtob:[{ id:trans, 在:借方, 出:银行, 价值:30 }] }, { btob:[{ id:基金, 在:银行, 出:银行, 数值:10 }], ctob:[{ id:基金, 在:信贷, 出:银行, 数值:10 }], dtob:[{ id:基金, 在:借方, 出:银行, 价值:30 }] }] 预期产出:

  [{
    "id": "trans",
    "in": "credit",
    "out": "bank",
    "value": 20
  },
  {
    "id": "fund",
    "in": "credit",
    "out": "bank",
    "value": 10
  }]

由于键ctob引用的对象符合您的选择要求,您只需执行以下操作:

const output = obj.map(entry => {
  return entry.ctob[0];
});
var obj=[{ btob:[{ id:trans, 在:银行, 出:银行, 数值:10 }], ctob:[{ id:trans, 在:信贷, 出:银行, 价值:20 }], dtob:[{ id:trans, 在:借方, 出:银行, 价值:30 }] }, { btob:[{ id:基金, 在:银行, 出:银行, 数值:10 }], ctob:[{ id:基金, 在:信贷, 出:银行, 数值:10 }], dtob:[{ id:基金, 在:借方, 出:银行, 价值:30 }] }]; 常量输出=obj.mapentry=>{ 返回分录.ctob[0]; };
console.logoutput 由于键ctob引用的对象符合您的选择要求,您只需执行以下操作:

const output = obj.map(entry => {
  return entry.ctob[0];
});
var obj=[{ btob:[{ id:trans, 在:银行, 出:银行, 数值:10 }], ctob:[{ id:trans, 在:信贷, 出:银行, 价值:20 }], dtob:[{ id:trans, 在:借方, 出:银行, 价值:30 }] }, { btob:[{ id:基金, 在:银行, 出:银行, 数值:10 }], ctob:[{ id:基金, 在:信贷, 出:银行, 数值:10 }], dtob:[{ id:基金, 在:借方, 出:银行, 价值:30 }] }]; 常量输出=obj.mapentry=>{ 返回分录.ctob[0]; };
console.logoutput 以下是一个功能型解决方案:

data.flatMap(obj => Object.values(obj).flatMap(arr => 
    arr.filter(t => t.in === "credit" && t.out === "bank")
));
const data=[{btob: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,out:bank,value:10}],ctob:[{id:fund:fund,in:credit,out:bank,value:10}],dtob id:fund:fund,in:debit:bank,value:30}; const result=data.flatMapobj=>Object.valuesobj.flatMaparr=>arr.filtert=>t.in==credit&t.out==bank;
console.logresult 以下是一个功能型解决方案:

data.flatMap(obj => Object.values(obj).flatMap(arr => 
    arr.filter(t => t.in === "credit" && t.out === "bank")
));
const data=[{btob: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,out:bank,value:10}],ctob:[{id:fund:fund,in:credit,out:bank,value:10}],dtob id:fund:fund,in:debit:bank,value:30}; const result=data.flatMapobj=>Object.valuesobj.flatMaparr=>arr.filtert=>t.in==credit&t.out==bank;
console.logresult 以下是您的解决方案:

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

以下是您的解决方案:

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

必须迭代数组和单个数组项的每个属性; 我以更可读的方式编写代码,添加了一些注释:

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

必须迭代数组和单个数组项的每个属性; 我以更可读的方式编写代码,添加了一些注释:

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

根据您当前的预期输出,不能在对象中使用相同的键。输出无效,因为不能在同一对象中使用相同的键。也许你想要一个对象数组?此外,您将t视为内部数组,尽管t是一个包含动态键ctob、btob以及该键的一个对象数组的对象。您可能希望在该数组上进行筛选。此外,您的对象在某些行的末尾丢失了。@briosheje是的,更新了output@manuman94抱歉和更新的更改根据当前的预期输出,您不能在对象中使用相同的键。输出无效,因为您不能在同一对象中使用相同的键。也许你想要一个对象数组?此外,您将t视为内部数组,尽管t是一个包含动态键ctob、btob以及该键的一个对象数组的对象。您可能希望在该数组上进行筛选。此外,您的对象在某些行的末尾丢失了。@briosheje是的,更新了output@manuman94道歉和更新的更改