Reputation: 128
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?
Upvotes: 3
Views: 19747
Reputation: 128
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)
Upvotes: 0
Reputation: 4971
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
Upvotes: 0
Reputation: 11
$scope.tasks = [
{ title: "Do the dishes" },
{ title: "Walk the dog" }
];
$scope.tasksDone = [];
for(var i in $scope.tasks){
$scope.tasksDone.push($scope.tasks[i]);
}
Upvotes: 1
Reputation: 1
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
$scope.tasksDone = [];
angular.forEach(tasks , function(value, key) {
this.push(key + ': ' + value);
},tasksDone);
};
Upvotes: 0
Reputation: 6803
$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]);
}
Upvotes: 3