Nicoleta Wilskon Nicoleta Wilskon - 1 month ago 13
AngularJS Question

Splicing one array with another array values

I have a function

displaySelectedRole()
and I have variables
$scope.Role
and
$scope.rolenames
.I need to remove all values available in
$scope.role
from
$scope.rolename.


$scope.role= ["A","B","C"];

$scope.rolename =["A","B","C","D","E"]


I need to splice the values and get
$scope.rolename = ["D","E"]


$scope.displaySelectedRole = function(role, index) {
debugger;
$scope.role.splice(RoleNames[index]);
console.log($scope.role);


I tried to use splice based on index , but problem is it given empty array values in console.

Answer

You can you Underscore.js's difference(), it purposes a method to substract arrays:

$scope.role = ["A","B","C"]; 
$scope.rolename = ["A","B","C","D","E"];
$scope.diff = _.difference($scope.rolename, $scope.role); // ["D","E"]
Comments