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

How to push json objects into arrays in angularJS

php中世界最好的语言
php中世界最好的语言Original
2018-04-12 15:55:322199browse

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}]
}

And my return format is

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

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);
 };

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!

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