1

I have array of objects named tickets and I want to pick some specific objects from tickets like number,desc and state and assign them to new array of objects say myarr. I'm writing the below code but it says number is undefined. What am I doing wrong ?

$scope.myarr=[{

  number:"",
  desc:"",
  state:""
  }

 ];


 for(var i=0;i<$scope.tickets.length;i++){

  $scope.myarr[i].number=$scope.tickets[i].number;
  $scope.myarr[i].desc=$scope.tickets[i].short_description;
  $scope.myarr[i].state=$scope.tickets[i].state;

 }
2
  • initialize your myarr $scope.myarr=[]; Commented Nov 2, 2016 at 7:33
  • What is the condition in your loop ? You said that you want to pick some specific objects, which one ? Commented Nov 2, 2016 at 7:34

3 Answers 3

1

You need do something like this.

$scope.myarr=[];
for(var i=0;i<$scope.tickets.length;i++){
  //Your Conditions
   var object={
      "number":$scope.tickets[i].number,
      "desc" :$scope.tickets[i].short_description,
      "state":$scope.tickets[i].state
   }
   $scope.myarr.push(object);
}
Sign up to request clarification or add additional context in comments.

1 Comment

was this helpful>>
0
$scope.myarr = [];
angular.forEach($scope.tickets, function(ticket) {
  this.push({number:ticket.number, state: ticket.state});
}, $scope.myarr);

Comments

0

If you don't need to support IE < 9, there is a handy function called map which is useful in this case

$scope.myarr = $scope.tickets.map(function(ticket) {
  // return the element to be inserted in the new array
  return {
    number: ticket.number,
    desc: ticket.short_description,
    state: ticket.state
  };
});

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.