0

Hi i am having a array like below

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

i need to convert to the below format

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

trying to convert by using for loop

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

getting array format as below:

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

any solution please

3
  • 3
    change as groups.push({"groupId":$scope.SelectedGroups[i]}); Commented Oct 6, 2017 at 4:58
  • Thanq all i have changed the groups.push({"groupId":$scope.Selectedgroups[i]}); and working great Commented Oct 6, 2017 at 5:07
  • @SudhirMN . but you don't need use any loop. You can use map(). Kindly check my answer Commented Oct 6, 2017 at 5:10

7 Answers 7

2

There is problem in this line :

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

Please change this to :

 groups.push({"groupId":$scope.Selectedgroups[i]});
Sign up to request clarification or add additional context in comments.

Comments

1

You should push the particular index value change it as

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

Comments

1

Access the array position using index, like $scope.Selectedgroups[i]

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

Comments

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

Comments

1

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);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">

</body>

Comments

1

Try this

<!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>

Comments

1

The problem is, You should push the particular index of value to your groups array. to all other answers are explains very well.


But I have an other answer ,You can do this by using map() function to reducing loop statement and few lines of code.

  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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.