簡體   English   中英

在javascript中迭代一個json對象,在數組中有一個數組

[英]Iterate over a json Object in javascript having an array inside array

我是網絡開發的新手。 我有一個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++) {
}

但它將消息的長度undefinedundefined 所以,我也希望在角度代碼中使用它。 我喜歡 -

ng-repeat="suggestion in jsonData.messages

任何人都可以解釋一下這個。 我搜索了很多並嘗試了,所以我問這個問題。

您需要嵌套循環並使用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
        });
    }
}
const flatten = [].concat.apply([], Object.values(jsonData.messages))
const result = [].concat.apply([], flatten.map(item => item.missingFields))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM