将 json 数据添加到现有数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19024427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:50:10  来源:igfitidea点击:

Add json data to existing array

javascriptjsonangularjs

提问by Gopesh

I have 2 json files,services.json and services_show.json.At page load am fetching the data from services.json and it working properly.On a button click,i need to fetch the contents from service_show.json and append to the services array but it does not work.

我有 2 个 json 文件,services.json 和 services_show.json。在页面加载时我从 services.json 获取数据并且它工作正常。单击按钮时,我需要从 service_show.json 获取内容并附加到服务数组,但它不起作用。

var beautyApp = angular.module('findbeauty', []);

beautyApp.controller('beautycntrl',function($scope,$http){

    $http.get('http://localhost/Find-Beauty/media/services.json').success(function(data) {
        $scope.services=data.services;
        $scope.services1=data.services1;
    });

    $scope.Add = function(){

        $http.get('http://localhost/Find-Beauty/media/services_show.json').success(function(data) {
            console.log(angular.toJson(data.services));
            $scope.services.push(data.services);

        });

    };

    $scope.ViewMore = function(){

});

Services.json

服务.json

{
"services":[
{
            "name": "Arun",
            "gender": "Damen",
            "duration": "1.5 Stunden",
            "price": "65,00",
            "imagepath": "media/images/prfilepic1.png",
            "percentage": "90%"
        },

    ],
    "services1":[

    {
            "name": "Schnitt & F?hnen",
            "gender": "Damen",
            "duration": "1.5 Stunden",
            "price": "65,00",
            "imagepath": "media/images/profilepic4.png",
            "percentage": "25%"
        },


    ]
}

service_show.json

service_show.json

{
"services":[
{
                "name": "Schnitt & F?hnen",
                "gender": "Damen",
                "duration": "1.5 Stunden",
                "price": "65,00",
                "imagepath": "media/images/profilepic4.png",
                "percentage": "5%"
            },

    ],
    "services1":[

    {
                "name": "Schnitt & F?hnen",
                "gender": "Damen",
                "duration": "1.5 Stunden",
                "price": "65,00",
                "imagepath": "media/images/prfilepic1.png",
                "percentage": "50%"
            },

    ]
}

How can i push the services_show.json data to $scope.services ? Any Help?

如何将 services_show.json 数据推送到 $scope.services ?任何帮助?

回答by Satpal

Array.prototype.push.apply()can be used for merging two arrays.

Array.prototype.push.apply()可用于合并两个数组。

Merge the second array into the first one

将第二个数组合并到第一个数组中

$scope.services.push.apply($scope.services, data.services);

回答by Chandermani

You need to push one item at a time, like

您需要一次推送一项,例如

angular.forEach(data.services,function(item) {
     $scope.services.push(item);
});

回答by Ronak Patel

You also can use concat: Here I have my own code: Please refer

您也可以使用concat:这里我有自己的代码:请参考

$http.get('store/LoadAlbums/', {'iCategoryID': iCategoryID}).then(function (data) {
      for (var i = 0; i < $scope.result.mainalbums.length; i++) {
         if($scope.result.mainalbums[i].iCategoryID == iCategoryID){
            $scope.result.mainalbums[i].albums = $scope.result.mainalbums[i].albums.concat(data.data.albums);
            $scope.result.mainalbums[i].totalAlbumCategory = $scope.result.mainalbums[i].albums.concat(data.data.totalAlbumCategory);
            $scope.result.mainalbums[i].loadmore = $scope.result.mainalbums[i].albums.concat(data.data.loadmore);
            $scope.result.mainalbums[i].newpage = $scope.result.mainalbums[i].albums.concat(data.data.newpage);
        }
       } });