在javascript中迭代一个json对象,在数组中有一个数组

时间:2018-03-09 14:11:24

标签: javascript jquery angularjs html5

我是网络开发的新手。我有一个json obj,就像

$scope.jsonData =     {
        "messages": {
            "A": [{
                "missingFields": [{
                    "RESPONSIBILITY": ""
                }],
                "temporaryId": 2,
                "messages": "",
                "id": 2,
                "content": ""
            }, {
                "missingFields": [{
                    "RESPONSIBILITY": ""
                }],
                "temporaryId": 3,
                "messages": "",
                "id": 3,
                "content": ""
            }, {
                "missingFields": [{
                    "RESPONSIBILITY": ""
                }],
                "temporaryId": 4,
                "messages": "",
                "id": 4,
                "content": ""
            }],
            "B": [{
                "missingFields": [{
                    "CITY": ""
                }, {
                    "PINCODE": ""
                }],
                "messages": "No Address details found.",
                "id": -1,
                "content": ""
            }]
        }
     }

现在我想迭代这个对象,我想得到"RESPONSIBILITY"字段。我试过这样 -

for (var i=0; i < $scope.jsonData.messages.length; i++) {
}

但是它将消息的长度设为undefined。所以,我也希望在角度代码中使用它。我喜欢 -

ng-repeat="suggestion in jsonData.messages

任何人都可以解释一下这个。我搜索了很多并尝试了,所以我问这个问题。

2 个答案:

答案 0 :(得分:2)

您需要嵌套循环并使用Object.keys来获取密钥

像:

for ( var k in jsonData.messages ) {
   jsonData.messages[k].forEach((v)=>{

       let tID = v.temporaryId || ""; //Get the temporaryId
       console.log( tID );

       v.missingFields.forEach((o)=>{
           //Object.keys(o)[0] <-- To get the first key of the object
           console.log( Object.keys(o)[0] );
       });
   });
}

要制作简化数组,您可以使用Object.valuesmap值。

let jsonData = {
  "messages": {
    "A": [{
      "missingFields": [{
        "RESPONSIBILITY": ""
      }],
      "temporaryId": 2,
      "messages": "",
      "id": 2,
      "content": "CONTENT 1"
    }, {
      "missingFields": [{
        "RESPONSIBILITY": ""
      }],
      "temporaryId": 3,
      "messages": "",
      "id": 3,
      "content": ""
    }, {
      "missingFields": [{
        "RESPONSIBILITY": ""
      }],
      "temporaryId": 4,
      "messages": "",
      "id": 4,
      "content": ""
    }],
    "B": [{
      "missingFields": [{
        "CITY": ""
      }, {
        "PINCODE": ""
      }],
      "messages": "No Address details found.",
      "id": -1,
      "content": ""
    }]
  }
}

let simplified = Object.values(jsonData.messages).map((v) => {
  let t = [];
  v.forEach(o => {
    t.push({
      temporaryId: o.temporaryId || "",
      missingFields: o.missingFields.map((x) => Object.keys(x)[0]),
      content: o.content || ""
    });
  });
  return t;
}).reduce((c, v) => c.concat(v), []);

console.log(simplified);

仅使用for循环。

var simplified = [];
for ( var k in jsonData.messages ) {
    for ( var i in jsonData.messages[k] ) {
        var temporaryId = jsonData.messages[k][i].temporaryId;
        var content = jsonData.messages[k][i].content;
        var missingFields = [];

        for ( var x in jsonData.messages[k][i].missingFields ) {
            for ( var y in jsonData.messages[k][i].missingFields[x] ) missingFields.push( y );
        }

        simplified.push({
          temporaryId: temporaryId,
          missingFields: missingFields,
          content: content
        });
    }
}

答案 1 :(得分:0)

const flatten = [].concat.apply([], Object.values(jsonData.messages))
const result = [].concat.apply([], flatten.map(item => item.missingFields))