admin管理员组

文章数量:1415100

Does Ember have a .difference function like underscore does? I have an ArrayController with a set of objects in each one. I want to subtract all the objects in ArrayController2 from ArrayController1 :

ArrayController1:
   1
   2
   3
   4

ArrayController2:
   2
   4

Then do the difference:

ArrayController1.difference(ArrayController2) => 1
                                                 3

Does Ember have a .difference function like underscore does? I have an ArrayController with a set of objects in each one. I want to subtract all the objects in ArrayController2 from ArrayController1 :

ArrayController1:
   1
   2
   3
   4

ArrayController2:
   2
   4

Then do the difference:

ArrayController1.difference(ArrayController2) => 1
                                                 3
Share Improve this question asked Jan 25, 2013 at 14:11 rickyduckrickyduck 4,08414 gold badges61 silver badges95 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I don't think there is an single method that will do that, but you could write a helper that essentially did the following:

array1.reject((function(item) {
  return array2.contains(item);
}), array2);

Just looping through array1 and rejecting anything that returns true for array2.contains().

5 years late but there's a puted function setDiff which you can use to achieve what you want.

import { setDiff } from '@ember/object/puted';
...
ArrayDifference: setDiff('ArrayController1', 'ArrayController2') // [1, 3]

https://www.emberjs./api/ember/2.18/functions/@ember%2Fobject%2Fputed/setDiff

本文标签: javascriptEmberjs equivalent of differenceStack Overflow