Home > Web Front-end > JS Tutorial > How to push json objects into arrays in angularJS

How to push json objects into arrays in angularJS

php中世界最好的语言
Release: 2018-04-12 15:55:32
Original
2341 people have browsed it

This time I will bring you angularJS jsonobjectHow to push to array, json in angularJS What are the precautions when objects are pushed into the array? The following is a practical case, let’s take a look.

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

The two arrays need to be integrated, and $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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese website Other related articles!

Recommended reading:

How to implement Immutable.js Undo and redo effect

How to deal with the compatibility of easyui date and time box in IE

The above is the detailed content of How to push json objects into arrays in angularJS. 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