Reputation: 13
I have to sets of Json got from the form the state data
objetSet1:
{id: 12, name: 'Foo Bar', email: '[email protected]'},
{id: 23, name: 'Bar Foo', email: '[email protected]'},
{id: 61, name: 'Barbell', email: '[email protected]'},
{id: 45, name: 'Joe Ocean', email: '[email protected]'}
objectSet2:
{ObjectId:15, name: 'someone', email: '[email protected]'},
{ObjectId: 23, name: 'sometwo', email: '[email protected]'},
{ObjectId: 72, name: 'seven ', email: '[email protected]'},
{ObjectId: 23, name: 'five ', email: '[email protected]'}
I was actually looking for a way to get this expression to be dynamic
objectSet2 = objectSet2.filter(object => object.ObjectId === '23')
instead of 23 static value, the value from objectSet1 corresponding
the result should contain Item with ids present on the first object set
expected output:
objectSet2:
{ObjectId: 23, name: 'sometwo', email: '[email protected]'},
{ObjectId: 23, name: 'five ', email: '[email protected]'}
Upvotes: 0
Views: 1366
Reputation: 14820
This combines Jonas Wilms and Ictus' answers in a way that I think is slightly clearer, and I've commented the code for future readers to know what's being done.
// Define Yoann Eddy's original two sets of objects, but as Arrays
let objectSet1 = [
{id: 12, name: 'Foo Bar', email: '[email protected]'},
{id: 23, name: 'Bar Foo', email: '[email protected]'},
{id: 61, name: 'Barbell', email: '[email protected]'},
{id: 45, name: 'Joe Ocean', email: '[email protected]'}
];
let objectSet2 = [
{ObjectId:15, name: 'someone', email: '[email protected]'},
{ObjectId: 23, name: 'sometwo', email: '[email protected]'},
{ObjectId: 72, name: 'seven ', email: '[email protected]'},
{ObjectId: 23, name: 'five ', email: '[email protected]'}
];
// Get just the ID numbers from the first set
let ids = new Set(objectSet1.map(o => o.id));
// Filter the second set, keeping only the objects that have
// an object.ObjectId that is in the first set of IDs
let result = objectSet2.filter(o => ids.has(o.ObjectId));
console.log(result);
Ictus's all-in-one line does the job, but will be harder to maintain simply because it's harder to understand by a colleague (or your future self)
Upvotes: 0
Reputation: 1567
Yoy were close, just needed to add a new filter like this:
objetSet1 = [{id: 12, name: 'Foo Bar', email: '[email protected]'},
{id: 23, name: 'Bar Foo', email: '[email protected]'},
{id: 61, name: 'Barbell', email: '[email protected]'},
{id: 45, name: 'Joe Ocean', email: '[email protected]'}];
objectSet2 = [{ObjectId:15, name: 'someone', email: '[email protected]'},
{ObjectId: 23, name: 'sometwo', email: '[email protected]'},
{ObjectId: 72, name: 'seven ', email: '[email protected]'},
{ObjectId: 23, name: 'five ', email: '[email protected]'}];
var result = objectSet2.filter((obj2)=>objetSet1.filter((obj1)=>obj1.id==obj2.ObjectId).length>0)
console.log(result);
Please note obj1.id==obj2.ObjectId
comparison. It's returning true when the count of positive matches on the inner filter is greater than zero. Then this is the answer for the outer filter.
Upvotes: 2
Reputation: 138477
You could create a set of ids from the first array:
const ids = new Set(objectSet1.map(object => object.id));
Then you can check wether objects from the second array are in that set
ids.has(object.ObjectId)
Upvotes: 0