Home  >  Article  >  Web Front-end  >  Push json object to instance in array in angular

Push json object to instance in array in angular

小云云
小云云Original
2018-03-01 09:49:211881browse

This article mainly shares with you examples of pushing json objects into arrays in angular. I hope it can help you.

In the project, the data format required by the API is


$scope.data = {
 "name":"zhangsan",
 "Menus": [{"id":1},{"id":2}]
}

and my return format is


 $scope.data=["name":"zhangsan"]
 $scope.selected = [1,2,3];

It is necessary to integrate the two arrays, in which $scope.selected must be converted into a json object first, and then the push operation is performed.

The code is as follows:


##

// 将menu数组转化为json格式
 self.convertJson = function (callback) {
  //传入数组为$scope.selected,每循环一遍就push一次
  angular.forEach($scope.selected, function (value, key) {
   $scope.data.Menus.push({
    'id':value
   });
  });
  callback($scope.data);
 };

Related recommendations:


Notes on the push method in JavaScript arrays

10 recommended articles about push

Analysis of usage of push method in Javascript array

The above is the detailed content of Push json object to instance in array in angular. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn