【问题标题】:How to iterate over a nested object array in Javascript如何在Javascript中迭代嵌套对象数组
【发布时间】:2019-04-03 08:33:52
【问题描述】:

如何遍历 Javascipt 中的嵌套对象数组?我有一个名为obj 的对象。我想检索increditoutbank 的对象。

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

这是数据:

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

预期输出:

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

【问题讨论】:

  • 根据您当前的预期输出,您不能在对象中使用相同的键
  • 输出无效,因为在同一个对象中不能有相同的键。也许您想要一个对象数组?此外,您正在考虑将 t 作为内部数组,尽管 t 是一个包含动态键 (ctob, btob) 的对象,并且对于该键,是一个对象数组。您可能希望改为过滤该数组。
  • 另外,您的对象在某些行的末尾缺少“,”。
  • @briosheje 是的,更新了输出
  • @manuman94 道歉和更新的变化

标签: javascript jquery arrays object


【解决方案1】:

这是一个函数式风格的解决方案:

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”意味着creedit to bank ",那么就不需要测试嵌套的 "credit" 和 "bank" 属性值了。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        这是您的解决方案:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-15
          • 1970-01-01
          • 2021-08-21
          • 2021-09-23
          • 1970-01-01
          • 2017-04-14
          相关资源
          最近更新 更多