I am trying to find the difference betweeen two arrays of objects using underscore js library.
-
underscore in coffeescript, every sain developers nightmare, so I guess simple plain old javascript, like this one is out ?adeneo– adeneo2014-07-25 21:00:03 +00:00Commented Jul 25, 2014 at 21:00
-
Can you help me convert that to coffeescript?compsci45000– compsci450002014-07-25 21:22:44 +00:00Commented Jul 25, 2014 at 21:22
Add a comment
|
1 Answer
Do you want to use the difference function of underscore? You can do this:
_.difference([1, 2, 3, 4, 5], [5, 2, 10])
this works in coffeescript.
EDIT
Using an array of objects and comparing the id property
arrayOne = [{id: 1}, {id: 2}]
arrayTwo =[{id: 2}, {id: 3}]
_.select arrayOne, (item) ->
!_.findWhere(arrayTwo, {id: item.id})
3 Comments
compsci45000
ya thats what i want to do but i want to do it for two arrays of objects not integers. Each object has a unique integer ID
Michael Oryl
Just beware: the order of the arrays matters when using _.difference(). It doesn't show you the items that don't belong to both, it shows you what you have left when you subtract the second array from the first.
EuAndreh
Awesome elegant solution. UnderscoreJS really rocks =]