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

时间:2019-04-03 08:33:53

标签: javascript jquery arrays object

如何遍历Javascipt中嵌套的对象数组?我有一个名为AWS::SQS::QUEUE的对象。我想检索docker network pruneobjincredit的对象。

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

5 个答案:

答案 0 :(得分:2)

这是一种实用的样式解决方案:

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” 表示 c 修改为 b ank ”,那么就无需测试嵌套的“贷方”和“银行”属性值。

答案 1 :(得分:1)

由于键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);

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

这是您的解决方案:

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

答案 4 :(得分:0)

由于源数组的结构,您不能直接使用过滤器。首先,您需要摆脱不必要的属性键并展平结构:

obj.map(entry => Object.values(entry)).flat(2).filter(entry => entry.in == 'credit' && entry.out == 'bank');

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 result = obj.map(entry => Object.values(entry)).flat(2).filter(entry => entry.in == 'credit' && entry.out == 'bank');

console.log(result);