Difference of two $scope array in angular js

ぃ、小莉子 提交于 2019-12-07 14:39:31

问题


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:


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);



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/23209408/difference-of-two-scope-array-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!