使用angularjs将键分配给数组

时间:2017-10-06 04:57:30

标签: javascript angularjs arrays

嗨,我有一个如下所示的数组

$scope.Selectedgroups =[183,184,24]

我需要转换为以下格式

 [{groupId:183},{groupId:184},{groupId:24}];

尝试使用for循环

进行转换
var groups=[]
        if($scope.Selectedgroups.length > 0){
            for(i=0; i< $scope.Selectedgroups.length; i++){
                groups.push({"groupId":$scope.Selectedgroups});
            }
        }

获取如下数组格式:

[{"groupId":[183,184,24]},{"groupId":[183,184,24]},{"groupId":[183,184,24]}]

任何解决方案

7 个答案:

答案 0 :(得分:2)

这一行存在问题:

 groups.push({"groupId":$scope.Selectedgroups});

请将此更改为:

 groups.push({"groupId":$scope.Selectedgroups[i]});

答案 1 :(得分:1)

使用索引访问数组位置,例如$scope.Selectedgroups[i]

for(i=0; i< $scope.Selectedgroups.length; i++){
      groups.push({"groupId":$scope.Selectedgroups[i]});
}

答案 2 :(得分:1)

您应该将特定的 index 值更改为

groups.push({"groupId":$scope.Selectedgroups[i]});

答案 3 :(得分:1)

use --  $scope.Selectedgroups[i]     

         var groups=[]
            if($scope.Selectedgroups.length > 0){
                for(i=0; i< $scope.Selectedgroups.length; i++){
                    groups.push({"groupId":$scope.Selectedgroups[i]});
                }
            }

答案 4 :(得分:1)

Mat inImg = imdecode(new Mat(imageData),IMREAD_UNCHANGED);
Mat outImg = Mat.zeros(inImg.size(), CV_8UC3).asMat();

UByteIndexer inIndexer = inImg.createIndexer();
UByteIndexer outIndexer = outImg.createIndexer();

for (int i = 0; i < inIndexer.rows(); i++) {
    for (int j = 0; j < inIndexer.cols(); i++) {
        int[] pixel = new int[4];
        try {
            inIndexer.get(i, j, pixel);
            if (pixel[3] == 0) { // transparency
                pixel[0] = pixel[1] = pixel[2] = 255;
            }
            outIndexer.put(i, j, pixel);
        } catch (IndexOutOfBoundsException e) {

        }
    }
}
var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
 $scope.Selectedgroups =[183,184,24];
  $scope.temp=[];
 $scope.Selectedgroups.forEach(function(e){
    $scope.temp.push({groupId:e});
 })
 $scope.Selectedgroups=$scope.temp;
 console.log($scope.Selectedgroups);
});

答案 5 :(得分:1)

  

问题是,您应该将特定的值索引推送到groups数组。所有其他答案都解释得很好。

但我有另一个答案 ,您可以使用map()函数来减少loop语句和几行代码。

  var app = angular.module('testApp',[]);
    app.controller('testCtrl',function($scope){
     $scope.Selectedgroups =[183,184,24];
     var groups=[]
      if($scope.Selectedgroups.length > 0){          
      groups = $scope.Selectedgroups.map(function(elem){    
         return {"groupId":elem};
      });
     }
     console.log(groups);
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <body ng-app="testApp" ng-controller="testCtrl">

    </body>

答案 6 :(得分:1)

试试这个

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<script type="text/javascript">
  var app = angular.module('myApp', []);

	app.controller('myCtrl', function ($scope,$filter) {
		$scope.Selectedgroups =[];
		var items = [183,184,24];
		for(i=0; i< items.length; i++){
			var obj = {};
			obj['groupId'] = items[i];
			$scope.Selectedgroups.push(obj);
		}
	});

</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
	{{Selectedgroups}}
</div>
</body>
</html>
&#13;
&#13;
&#13;