如何向javascript对象添加元素?

时间:2016-01-21 16:20:03

标签: javascript jquery json

我有一个服务对象,我需要将它的日志附加到ServiceLogEntries.ID等于ServiceID的位置。

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

试试这个

&#13;
&#13;
$(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);

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

答案 2 :(得分: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;
                }
            }
        }