Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
Gopesh Avatar asked Sep 26 '13 09:09

Gopesh


People also ask

How do I add data to an existing JSON object?

Use push() method to add JSON object to existing JSON array in JavaScript. Just do it with proper array of objects .

How do I push JSON data into an array in node JS?

To push JSON objects into an array in local storage, we can push the object into an array, then we can stringify that array and put it into local storage. For instance, we can write: const a = []; const obj = { foo: 'bar' } a. push(obj); localStorage.

How do I place a JSON object into another JSON object?

Use Spread syntax to add JSON object to another JSON object in JavaScript. And use the push() method to add a JSON array at end of an array.

How do I add a JSON string to an existing JSON file in Java?

In the initial step, we can read a JSON file and parsing to a Java object then need to typecast the Java object to a JSonObject and parsing to a JsonArray. Then iterating this JSON array to print the JsonElement. We can create a JsonWriter class to write a JSON encoded value to a stream, one token at a time.


1 Answers

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);
like image 118
Satpal Avatar answered Oct 08 '22 06:10

Satpal