Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 4
    I think it's a good solution performance-wise, especially as lodash and underscore keep battling for the best implementation. Also, it's IE6-compatible. Commented Sep 7, 2012 at 18:44
  • 4
    Beware, this implementation will not work for arrays of objects. See stackoverflow.com/q/8672383/14731 for more information. Commented Dec 31, 2012 at 19:52
  • 1
    As one of the answers mentions there, it works if it's the same object, but not if two objects have the same properties. I think that's okay as notions of equality vary (e.g. it could also be an "id" attribute in some apps). However, it would be good if you could pass in a comparison test to intersect(). Commented Jan 1, 2013 at 2:12
  • For posterity: Lodash now has _.differenceBy() which takes a callback to do the comparison; if you're comparing objects you can drop in a function that compares them however you need. Commented Sep 8, 2016 at 21:13
  • 3
    Be careful if the order of arguments reversed, it will not work. eg. _.difference( [5, 2, 10], [1, 2, 3, 4, 5]); can not get the diff Commented Oct 4, 2016 at 15:32