Angular JS中两个数组之间的差异

时间:2021-07-01 12:56:43

Consider following arrays:

考虑以下数组:

array1 = ['a','b'];
array2 = ['a','b','c','d'];

I need to extract the difference. So my resulting array should look something like,

我需要提取差异。所以我得到的数组看起来应该是这样的,

array3 = ['c','d'];

If an element is present in array1 then it should be poped from array2. I am looking for solutions more angular way,is there any directive available?

如果array1中存在一个元素,那么它应该从array2中加载。我正在寻找更有棱角的解决方案,有没有可用的指令?

1 个解决方案

#1


1  

In my opinion you can use underscore or lodash library for such tasks. for example in underscore you can done it through this simple code :

在我看来,你可以使用下划线或lodash库来完成这些任务。例如,在下划线中,您可以通过这个简单的代码完成它:

difference_.difference(array, *others)

Similar to without, but returns the values from array that are not present in the other arrays.

与without类似,但返回数组中不存在于其他数组中的值。

_.difference([1, 2, 3, 4, 5], [5, 2, 10]); => [1, 3, 4]

_.difference([1,2,3,4,5],[5,2,10]); => [1,3,4]

underscore annotated source

强调注释源

#1


1  

In my opinion you can use underscore or lodash library for such tasks. for example in underscore you can done it through this simple code :

在我看来,你可以使用下划线或lodash库来完成这些任务。例如,在下划线中,您可以通过这个简单的代码完成它:

difference_.difference(array, *others)

Similar to without, but returns the values from array that are not present in the other arrays.

与without类似,但返回数组中不存在于其他数组中的值。

_.difference([1, 2, 3, 4, 5], [5, 2, 10]); => [1, 3, 4]

_.difference([1,2,3,4,5],[5,2,10]); => [1,3,4]

underscore annotated source

强调注释源