3


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?

9
  • 1
    take out element from array, get its index & splice it, after that push it to another array Commented Jul 2, 2016 at 10:03
  • How do i take it out from this array? Commented Jul 2, 2016 at 10:04
  • pass unique value to function, loop through the list & find element by unique id Commented Jul 2, 2016 at 10:05
  • Do you have an example for that? Commented Jul 2, 2016 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. Commented Jul 2, 2016 at 10:09

6 Answers 6

3
$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]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

it only pushes the first item of the array.
It was just an example use index .
you have to provide index like $scope.addToTasksdone(1)
1
$scope.tasks.push(yourObject)

this question was aked before here

Comments

1
$scope.tasks = [
    { title: "Do the dishes" },
    { title: "Walk the dog" }
]; 
$scope.tasksDone = [];
for(var i in $scope.tasks){
    $scope.tasksDone.push($scope.tasks[i]);
}

Comments

0
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
 $scope.tasksDone = [];
angular.forEach(tasks , function(value, key) {
  this.push(key + ': ' + value);
},tasksDone);
};

Comments

0

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

Comments

0

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).

Ofcourse also in the HTML for example: ng-click="addToTasksDone($index)

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.