Sajith Sajith - 10 days ago 6
Javascript Question

How to append to a json object in angular js

I am having an object like this

$scope.releases = [{name : "All Stage",active:true}];


I need to append more data to it

[
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]


So the final data should be like this

[
{name : "All Stage",active:true}
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]


I tried the following code. But it is not appending.

app.controller('MainCtrl', function($scope) {
// I am having an object like this
$scope.releases = [{name : "All Stage",active:true}];
// I need to appned some more data to it
$scope.releases = [
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]
});


Pluker Link : http://plnkr.co/edit/gist:3510140

Answer
  $scope.releases = [{name : "All Stage",active:true}];
  // Concatenate the new array onto the original
  $scope.releases = $scope.releases.concat([
    {name : "Development",active:false},
    {name : "Production",active:false},
    {name : "Staging",active:false}
  ]);
Comments