Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two arrays of objects coffeescript using underscore js

I am trying to find the difference betweeen two arrays of objects using underscore js library.

like image 996
compsci45000 Avatar asked Jul 25 '14 20:07

compsci45000


People also ask

How will you differentiate between arrays and objects in JavaScript?

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.

What is underscore js used for?

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.


1 Answers

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})
like image 163
Gabriel Avatar answered Nov 14 '22 22:11

Gabriel