从两个http请求合并对象AngularJS

时间:2017-11-30 08:57:09

标签: javascript arrays angularjs merge

我尝试将两个HTTP.get请求合并到一个$scope中,因为我想在同一个ng-repeat表中显示数据。我在AngularJS应用程序中使用这样的链式承诺:

AngularJS:

function getContainer() {
            $http.get("/api/containers")
                .then(function (response) {
                    $scope.GTMcontainersAccount = response.data;
                    $scope.loading = false;


                    // Retrive containerId and AccountID then request to show Counter of how many Tags, Triggers and Variables are available in each Container
                    angular.forEach($scope.GTMcontainersAccount, function (key) {
                        angular.forEach(key, function (keydata) {
                            $scope.accountId = keydata.accountId;
                            $scope.containerId = keydata.containerId;
                            $http.get("/api/tags", {
                                params: {
                                    "param1": $scope.accountId,
                                    "param2": $scope.containerId
                                }
                            }).then(function (responses) {
                                $scope.tags_counter = responses.data[""]['tags'];
                                $scope.trigger_counter = responses.data[""]['trigger'];
                                $scope.variable_counter = responses.data[""]['variable'];


                            })
                        })

                    })
                })
        }

解释我在代码中做了什么:
1)http.get(/api/containers)给我一些JSON格式的数据 2)angular.forEach通过键盘迭代 3)angular.forEach迭代通过值(键是动态的,这就是为什么有2个angular.forEach。
4)http.get(/api/tags)我在参数中发送一些数据并得到一些结果数据

5)这是困难的部分,我想在$scope.GTMcontainersAccount上使用ng-repeat但不能将$scope.GTMcontainersAccount$scope.tags_counter$scope.trigger_counter$scope.variable_counter合并

我在$scope.GTMcontainersAccount中的数据:

{
    "Account2": [{
        "accountId": "1746756959",
        "containerId": "7454209",
    }, {
        "accountId": "1746756959",
        "containerId": "7519183",
    }],
    "Account 1": [{
        "accountId": "1766143525",
        "containerId": "7483653",
    }]
}

我在$scope.tags_counter:中的数据(我已经删除了很多,因为它很长,最重要的是最后一个关键:值)

{
    "0": {
        "accountId": "****",
        "containerId": "*****",
        "tag": [{
            "accountId": "*****",
            "containerId": "********",
            "name": "Test Tag"
        }, {
            "accountId": "***",
            "containerId": "***",
            "name": "Classic Google Analytics"
        }, {
            "accountId": "***",
            "containerId": "***",
            "name": "contentEngagement"
        }],
        "trigger": [{
            "accountId": "***",
            "containerId": "***",
            "name": "Trigger 1"
        }, {
            "accountId": "***",
            "containerId": "***",
            "name": "Trigger 2"
        }, {
            "accountId": "***",
            "containerId": "***",
            "name": "Trigger 3"
        }],
        "variable": [{
            "accountId": "***",
            "containerId": "***",
            "name": "Google Analytics Settings"
        }, {
            "name": "Protocol"
        }, {
            "accountId": "*******",
            "containerId": "****",
            "name": "Lookup Table"
        }],
        "builtInVariable": [{
            "accountId": "*******",
            "containerId": "******",
            "name": "Page URL",
            "path": null,
            "type": "pageUrl",
            "workspaceId": null
        }, {
            "accountId": "**",
            "containerId": "**",
            "name": "Page Hostname",
            "path": null,
            "type": "pageHostname",
            "workspaceId": null
        }, {
            "accountId": "***",
            "containerId": "***",
            "name": "Page Path",
            "path": null,
            "type": "pagePath",
            "workspaceId": null
        }, {
            "accountId": "***",
            "containerId": "***",
            "name": "Referrer",
            "path": null,
            "type": "referrer",
            "workspaceId": null
        }, {
            "accountId": "***",
            "containerId": "****",
            "name": "Event",
            "path": null,
            "type": "event",
            "workspaceId": null
        }]
    },
    "": {                 <----- THIS is what i want to merge
        "tags": 3,
        "trigger": 3,
        "variable": 3
    }
}

所以在合并之后我希望$scope.GTMcontainersAccount看起来像这样: 的最终

{
    "Account2": [{
        "accountId": "1746756959",
        "containerId": "7454209",
    }, {
        "accountId": "1746756959",
        "containerId": "7519183",
    }],
    "Account 1": [{
        "accountId": "1766143525",
        "containerId": "7483653",
    }],
    "Counter" : {
        "tags": 3,
        "trigger": 3,
        "variable": 3
}

更新: enter image description here

1 个答案:

答案 0 :(得分:2)

我为你带来了一个小提琴。

看起来有点乱,但效果很好。

我希望这对你有所帮助。 :)

Merge Object

$scope.GTMcontainersAccount['Counter'] = Object.values($scope.tags_counter)[(Object.keys($scope.tags_counter).length-1)];

添加一些解释,

(Object.keys($scope.tags_counter).length-1)

上面是获取对象的最后一个索引。

Object.keys($scope.tags_counter)会返回keys的所有$scope.tags_counter

Object.values($scope.tags_counter)[ index ]会返回values的所有$scope.tags_counter

因此,当您知道所需的值索引时,很容易获得任何对象的值。

<强>更新

因为你的照片我很困惑。

为什么所有行都有相同的'Counter'对象。

您是否将“$ scope.tags_counter”中的相同对象放入不同的对象中是值得怀疑的。

如果您要获取的密钥始终为空字符串,如""

然后您可以使用$scope.tags_counter[""];

请检查this other fiddle

或者你可以制作出包含整个物品的小提琴吗?

<强> UPDATE2

为了更加确定你的目的,我再次成了这个小提琴。

Please check this fiddle

如果这也不适合您的意图,请与我分享其他信息。