Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersection on Array of Objects #926

Closed
jintoppy opened this issue Feb 4, 2015 · 5 comments
Closed

Intersection on Array of Objects #926

jintoppy opened this issue Feb 4, 2015 · 5 comments
Labels

Comments

@jintoppy
Copy link

jintoppy commented Feb 4, 2015

Is it possible to include the intersection on array of objects as a method in the library itself?

@jdalton
Copy link
Member

jdalton commented Feb 4, 2015

You can customize the matching of values by customizing _.indexOf.

var objects = [{ 'a': 1 }, { 'a': 2 }],
    other = [{ 'a': 2 }, { 'a': 3 }];

var _ = require('lodash').runInContext();
_.indexOf = _.findIndex;
_.intersection(objects, other);
// => [{ 'a': 2 }]

@jdalton jdalton closed this as completed Feb 4, 2015
@vigneshnrfs
Copy link

Hi.

I would like to do intersection based on a particular property in the object. For example, id in the below example.

var objects = [{ 'id': 1,data:[{...}] }, { 'id': 2,data:[{...}] }],
    other = [{ 'id': 2 ,data:[{...}] }, { 'id': 3,data:[{...}]  }];

The intersection of the above two objects based on id should be {'id':2,data:[{...}]
Is it possible to accomplish it ?

@jdalton
Copy link
Member

jdalton commented Oct 26, 2015

@vigneshnrfs
In our edge master branch in prep for v4 I've added _.intersectionBy so you could do _.intersectionBy(objects, other, 'id')

@smonff
Copy link

smonff commented Apr 11, 2018

For people who would end up here after Googling, please read this Stack Overflow answer.

@lock
Copy link

lock bot commented Jun 2, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

4 participants