Question

Is there a way to return the difference between two array present in a scope in angularjs

For example,

 $scope.user1 = ['a', 'b'];
 $scope.user2 = ['a', 'b', 'c', 'd'];

Difference of these two should give me an another as $scope.user3= ['c','d']

Was it helpful?

Solution

Underscore.js has the difference method for this.

http://underscorejs.org/#difference

$scope.user1 = ['a', 'b'];
$scope.user2 = ['a', 'b', 'c', 'd'];
$scope.user3 = _.difference($scope.user2, $scope.user1);

OTHER TIPS

Angular can't do anything about it. Underscore.js is good but I prefer Lo-Dash

Lo-Dash is a utility library delivering consistency, customization, performance, & extras. And Lo-Dash can

$scope.user1 = ['a', 'b'];
$scope.user2 = ['a', 'b', 'c', 'd'];
$scope.user3 = _.difference($scope.user2, $scope.user1); // ['c','d']

There is nothing there in angularjs. You can look at underscore library difference method, or may create your own method to calculate the difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top