0

I am using angularjs, I have one for loop and I get the loop values into one variable called Name .Now my need is that variable should store into scope array selection . How to achieve this.

code:

$scope.selection = [];
     for(var i=0 ;i<$scope.Streams.length;i++)
                 {
var Name=$scope.Streams[i].Name;
  }
            

here i add the dynamic value of name into scope array.

I need output of my scope variable like this

enter image description here

5
  • 1
    your question is unclear..please be specified what you want. Commented May 6, 2017 at 6:19
  • now i update my code let me check @pengyy Commented May 6, 2017 at 6:20
  • I need output of my scope variable like this ... what does this mean? Commented May 6, 2017 at 6:20
  • i get my $scope.selection value like this mentioned in image Commented May 6, 2017 at 6:21
  • my loop is running each time loop running time i get value in variable name now that name will store into scope selection Commented May 6, 2017 at 6:23

2 Answers 2

2

Guess you want to add items of $scope.Streams to $scope. selection. you can use Array.push.

$scope.selection = [];
// option1
for(var i=0 ;i<$scope.Streams.length - 1;i++) {
  $scope.selection.push($scope.Streams[i]); 
}
// option2 (with new instance)
$scope.selection = $scope.Streams.slice();
// option3
$scope.selection = $scope.Streams;
Sign up to request clarification or add additional context in comments.

3 Comments

it's working fine but show only value i need the add column into scope.selection how to to this
@jose so you want the whole object be pushed not just the name of the Object?
@jose see my updated answer. I give you 3 options, pick the one you like.
1

Try this single line of code with Array.map() method :

$scope.selection = $scope.Streams.map(item => { return {"Name":item.Name} });

DEMO

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {

    $scope.Streams = [
     {
       "id":1,
       "Name":"stream1"
     },
     {
       "id":2,
       "Name":"stream2"     
     },
     {
       "id":3,
       "Name":"stream3"     
     }
    ];
    
    $scope.selection = $scope.Streams.map(item => { return {"Name":item.Name} });
    
console.log($scope.selection);

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
</div>

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.