Home > Web Front-end > JS Tutorial > body text

How to use the method of pushing json objects into arrays in angular

亚连
Release: 2018-06-04 10:48:55
Original
2472 people have browsed it

Now I will share with you a method of pushing angular json objects into arrays. It has a good reference value and I hope it will be helpful to everyone.

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

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

and my return format is

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

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);
 };
Copy after login

The above is what I compiled for everyone. I hope that in the future It will be helpful to everyone.

Related articles:

How to use filters in the Vue2.0 series?

How to get the value in select in vue.js (detailed tutorial)

How to get it in Vue Use slot to implement slot distribution content

The above is the detailed content of How to use the method of pushing json objects into arrays in angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template