I am trying to find the difference betweeen two arrays of objects using underscore js library.
Both objects and arrays are considered “special” in JavaScript. Objects represent a special data type that is mutable and can be used to store a collection of data (rather than just a single value). Arrays are a special type of variable that is also mutable and can also be used to store a list of values.
Underscore. js is a utility library that is widely used to deal with arrays, collections and objects in JavaScript. It can be used in both frontend and backend based JavaScript applications. Usages of this library include filtering from array, mapping objects, extending objects, operating with functions and more.
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With