Add and remove elements between arrays in angular

Using an angular array, how do I add and remove elements between two arrays? I $scope.resultsand $scope.listan array of results - results WebAPI call, and I allow the user to select the items that they want to add in the second array. How can I add from the first to the second and remove from the first at the same time?

    angular.forEach($scope.results, function (item) {
        if (item.selected) {
            $scope.list.push(item);
            //CODE TO REMOVE item from $scope.results here.
        };
    });

Also, if I did a second search and tried to add the same element from the first array to my second array (which already had this user), how can I prevent duplicates from being added to the second array (list)? enter image description here.

below is a sample of objects that you wanted to pass between arrays. id field is an indicator of uniqueness.

+4
1

angular.forEach. , . .

            angular.forEach($scope.results, function (item, index) {
                if (item.selected) {
                    $scope.list.push(item);
                    $scope.results.splice(index, 1);
                };
            });

, angular.forEach, . , , .

, .

var len = $scope.results.length;
while (len--) {
    var item = $scope.results[len];
    if (item.selected) {
       $scope.list.push(item);
       $scope.results.splice(len, 1);
    };
}

.

+3

All Articles