3

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']

1
  • It doesn't have anything to do with angular, you are asking about the basic array concepts. Commented Apr 22, 2014 at 4:05

3 Answers 3

10

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

Comments

3

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']

Comments

1

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

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.