Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →


I am programming an app with AngularJS and wanted to know how to push an item from one array into another array.

Here is sample code:

$scope.tasks = [
     {title: "Do the dishes"},
     {title: "Walk the dog"},
];
$scope.addToTasksDone = function() {// IM FAILNG HERE};
$scope.tasksDone = [];

How can I push the item with value "Do the dishes" to the tasksDone array?

share|improve this question
1  
take out element from array, get its index & splice it, after that push it to another array – Pankaj Parkar Jul 2 at 10:03
    
How do i take it out from this array? – Julian Be Jul 2 at 10:04
    
pass unique value to function, loop through the list & find element by unique id – Pankaj Parkar Jul 2 at 10:05
    
Do you have an example for that? – Julian Be Jul 2 at 10:06
    
You could avoid playing with arrays and add an attribute "finished: true" to you task object. This would be better design in my opinion. – gyc Jul 2 at 10:09
up vote 2 down vote accepted
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
$scope.tasksDone = [];
$scope.addToTasksDone = function(index) {// IM FAILNG HERE};
$scope.tasksDone.push($scope.tasks[index]);
}
share|improve this answer
    
it only pushes the first item of the array. – Julian Be Jul 2 at 10:09
    
It was just an example use index . – Piyush.kapoor Jul 2 at 10:11
    
It doesnt work for me aswell, it pushes "undefined" – Julian Be Jul 2 at 20:10
    
you have to provide index like $scope.addToTasksdone(1) – Piyush.kapoor Jul 2 at 20:12
$scope.tasks.push(yourObject)

this question was aked before here

share|improve this answer
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
 $scope.tasksDone = [];
angular.forEach(tasks , function(value, key) {
  this.push(key + ': ' + value);
},tasksDone);
};
share|improve this answer
$scope.tasks = [
    { title: "Do the dishes" },
    { title: "Walk the dog" }
]; 
$scope.tasksDone = [];
for(var i in $scope.tasks){
    $scope.tasksDone.push($scope.tasks[i]);
}
share|improve this answer

you can use the following code to push some specific value to the new array

$scope.tasks = [
     {title: "Do the dishes"},
     {title: "Walk the dog"},
];
$scope.tasksDone = [];
$scope.addToTasksDone = function(specificValue) {
                           // IM FAILNG HERE};
     tasks.forEach( (object) => {
          if ( object[title] === specificValue ) {
                $scope.tasksDone.push(object);
          }
     });
}

Above code will push each object which contain value as specific Value... You can invoke addToTasksDone by passing "Do the dishes" value as parameter.

Sample invocation

$scope.addToTasksDone("Do the dishes");

Regards

Ajay

share|improve this answer

This is the right code.
I have the list inside a ng-repeat so i have to change "$scope.addToTasksDone(index)" to "$scope.addToTasksDone($index)".
Of course also in the Html for example -> ng-click="addToTasksDone($index).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.