2016-01-21 77 views
-1

我有一個服務對象,我需要將它的日誌附加到ServiceLogEntries.ID等於ServiceID的地方。如何將元素添加到JavaScript對象?

var Services = { 
      "result": [ 
       { 
        "ID": 150779, 
        "ServiceRenderedID": 1, 
        "ServiceTypeName": "Health Risk Assessment", 
        "PackageID": 13 
       }, 
       { 
        "ID": 150778, 
        "ServiceRenderedID": 1, 
        "ServiceTypeName": "Health Risk Assessment", 
        "PackageID": 13 
       } 
      ] 
     }; 

var ServiceLogEntries = { 
      "ServiceLogEntries": [ 
       { 
        "LogID": 229500.0, 
        "LogStamp": "2015-12-08T18:29:16.137", 
        "LogServerUser": "Webuser", 
        "LogActionType": "I", 
        "ID": 150779 
       }, 
       { 
        "LogID": 229501.0, 
        "LogStamp": "2015-12-08T18:30:07.32", 
        "LogServerUser": "Webuser", 
        "LogActionType": "U", 
        "ID": 150779 
       }, 
       { 
        "LogID": 229500.0, 
        "LogStamp": "2015-12-08T18:29:16.137", 
        "LogServerUser": "Webuser", 
        "LogActionType": "I", 
        "ID": 150778 
       }, 
       { 
        "LogID": 229501.0, 
        "LogStamp": "2015-12-08T18:30:07.32", 
        "LogServerUser": "Webuser", 
        "LogActionType": "U", 
        "ID": 150778 
       } 
      ] 
     }; 

結果應該看起來像下面的代碼,其中元素「ServiceLogEntires」是通過附加相應的項目創建的。

var Service = { 
      "result": [ 
       { 
        "ID": 150779, 
        "ServiceRenderedID": 1, 
        "ServiceTypeName": "Health Risk Assessment", 
        "PackageID": 13 
       }, 
       { 
        "ID": 150778, 
        "ServiceRenderedID": 1, 
        "ServiceTypeName": "Health Risk Assessment", 
        "PackageID": 13, 
        "ServiceLogEntries": [ 
         { 
          "LogID": 229500.0, 
          "LogStamp": "2015-12-08T18:29:16.137", 
          "LogServerUser": "Webuser", 
          "LogActionType": "I", 
          "ID": 150778 
         }, 
         { 
          "LogID": 229501.0, 
          "LogStamp": "2015-12-08T18:30:07.32", 
          "LogServerUser": "Webuser", 
          "LogActionType": "U", 
          "ID": 150778 
         } 
        ] 
       } 
      ] 

我開始通過這是很容易做到的對象循環,但我需要得到整個元素"ServiceLogEntries": [{...}]

for (var i = 0; i < Service.result.length; i++) { 
      var parent = Service.result[i]; 
      for (var a = 0; a < ServiceLogEntries.ServiceLogEntries.length; a++) { 
       var child = ServiceLogEntries.ServiceLogEntries[a]; 

       if (child.ID === parent.ID) { 
        parent.push(child); 
       } 
      } 
     } 
+0

和WH結果你會得到什麼?你知道jsfiddle嗎? – helle

+0

https://jsfiddle.net/t3twxgfs/ –

回答

2
Services.result.forEach(function (service) { 
    service.ServiceLogEntries = ServiceLogEntries.ServiceLogEntries.filter(function (entry) { 
     return entry.ID === service.ID; 
    }); 
}); 
1

你有像字典對象,它可不是陣列中,JavaScript,因此你的循環變得

for (var i = 0; i < Service.result.length; i++) { 
      var parent = Service.result[i]; 
      for (var a = 0; a < ServiceLogEntries.ServiceLogEntries.length; a++) { 
       var child = ServiceLogEntries.ServiceLogEntries[a]; 

       if (child.ID === parent.ID) { 
        parent.ServiceLogEntries = child; 
       } 
      } 
     } 
2

試試這個

$(function() { 
 

 
    var Services = { 
 
    "result": [{ 
 
     "ID": 150779, 
 
     "ServiceRenderedID": 1, 
 
     "ServiceTypeName": "Health Risk Assessment", 
 
     "PackageID": 13 
 
    }, { 
 
     "ID": 150778, 
 
     "ServiceRenderedID": 1, 
 
     "ServiceTypeName": "Health Risk Assessment", 
 
     "PackageID": 13 
 
    }] 
 
    }; 
 

 
    var ServiceLogEntries = { 
 
    "ServiceLogEntries": [{ 
 
     "LogID": 229500.0, 
 
     "LogStamp": "2015-12-08T18:29:16.137", 
 
     "LogServerUser": "Webuser", 
 
     "LogActionType": "I", 
 
     "ID": 150779 
 
    }, { 
 
     "LogID": 229501.0, 
 
     "LogStamp": "2015-12-08T18:30:07.32", 
 
     "LogServerUser": "Webuser", 
 
     "LogActionType": "U", 
 
     "ID": 150779 
 
    }, { 
 
     "LogID": 229500.0, 
 
     "LogStamp": "2015-12-08T18:29:16.137", 
 
     "LogServerUser": "Webuser", 
 
     "LogActionType": "I", 
 
     "ID": 150778 
 
    }, { 
 
     "LogID": 229501.0, 
 
     "LogStamp": "2015-12-08T18:30:07.32", 
 
     "LogServerUser": "Webuser", 
 
     "LogActionType": "U", 
 
     "ID": 150778 
 
    }] 
 
    }; 
 

 
    $.each(Services.result, function(i, service) { 
 
    service['ServiceLogEntries'] = ServiceLogEntries.ServiceLogEntries.filter(function(elem) { 
 
     return elem.ID === service.ID; 
 
    }); 
 
    }) 
 

 
    console.log(Services); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

'.filter()'通過返回true或false來工作,你可以將它減少到'return elem.ID === service.ID;'。 – Tomalak

+0

哦,那是真的!謝謝......與地圖功能混淆 – Vanojx1

+1

另外,'Services.result [i]'和'service'是一樣的。 ;) – Tomalak