Gopesh
Gopesh

Reputation: 3950

Add json data to existing array

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.

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

{
"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

{
"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?

Upvotes: 12

Views: 43537

Answers (3)

Satpal
Satpal

Reputation: 133403

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

Merge the second array into the first one

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

Upvotes: 25

Ronak Patel
Ronak Patel

Reputation: 3444

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

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

Upvotes: 0

Chandermani
Chandermani

Reputation: 42669

You need to push one item at a time, like

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

Upvotes: 7

Related Questions