Reputation: 3444
Here I have loaded data from angularjs as below:
{"mainalbums":
[{"iCategoryID":"1","vCategoryName":"Hip Hop",
"albums":
[{"iAlbumID":"23","vAlbumName":"Mychoice 1"},{"iAlbumID":"30","vAlbumName":"Mychoice 2"}],
"totalAlbumCategory":2,
"loadmore":"false",
"newpage":1},
{"iCategoryID":"3","vCategoryName":"Country",
"albums":
[{"iAlbumID":"31","vAlbumName":"Dil Dhadkne Do 1"},
{"iAlbumID":"31","vAlbumName":"Dil Dhadkne Do 2"}],
"totalAlbumCategory":1,
"loadmore":"false",
"newpage":1},
{"iCategoryID":"4","vCategoryName":"Remixes",
"albums":[
{"iAlbumID":"25","vAlbumName":"Love me thoda aur 1"},{"iAlbumID":"26","vAlbumName":"Love me thoda aur 2"},
"totalAlbumCategory":4,"loadmore":"true","newpage":1}]}
Here, mainalbums have albums which have totalAlbumCategory, loadmore, newpage. I want to update second albums from the data. How can I do it in angularjs?
Upvotes: 1
Views: 158
Reputation: 3444
Finally,
I got answer by below (and Thank you Samuel-Jaeschke): You also can use 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);
}
} });
Happie!!
Upvotes: 1
Reputation: 1296
These are all Javascript objects and arrays - you can simply access them as such.
For example, if the object in your question was called data
, you could get the second (index 1
) object's albums
array using the following:
data.mainalbums[1].albums
Since albums
is an array, you can append using the push()
method like so:
data.mainalbums[1].albums.push({"iAlbumID":"31","vAlbumName":"Dil Dhadkne Do 3"});
Edit:
In your example, you don't have a category id 2
, which would currently cause you problems since you have to access categories by their array index, rather than actual id. Since your categories have IDs, you may find it easier to organise your albums by changing mainalbums
to be an object, rather than an array.
For example, if you were to define your object like this:
{"mainalbums": {
"1": {
"iCategoryID":"1", "vCategoryName":"Hip Hop",
"albums": [
{"iAlbumID":"23","vAlbumName":"Mychoice 1"},
{"iAlbumID":"30","vAlbumName":"Mychoice 2"}
],
"totalAlbumCategory":2,
"loadmore":"false",
"newpage":1
},
"3": {
"iCategoryID":"3", "vCategoryName":"Country",
"albums": [
{"iAlbumID":"31","vAlbumName":"Dil Dhadkne Do 1"},
{"iAlbumID":"31","vAlbumName":"Dil Dhadkne Do 2"}
],
"totalAlbumCategory":1,
"loadmore":"false",
"newpage":1
},
"4": {
"iCategoryID":"4","vCategoryName":"Remixes",
"albums":[
{"iAlbumID":"25","vAlbumName":"Love me thoda aur 1"},
{"iAlbumID":"26","vAlbumName":"Love me thoda aur 2"}
],
"totalAlbumCategory":4,
"loadmore":"true",
"newpage":1
}
}
Then you could access it using category ids like this:
data.mainalbums['3'].albums
This of course depends on whether you have control over how this object is constructed.
(Note: mainalbums is now an object, not an array, but this notation is required since we need to quote the number. JavaScript won't let you use numbers as plain variable names. However, this is still the preferred way to do things.)
Upvotes: 1