问题
I have to sets of Json got from the form the state data
objetSet1:
{id: 12, name: 'Foo Bar', email: 'foo@bar.com'},
{id: 23, name: 'Bar Foo', email: 'bar@foo.com'},
{id: 61, name: 'Barbell', email: 'barbell@mail.com'},
{id: 45, name: 'Joe Ocean', email: 'joe@ocean.com'}
objectSet2:
{ObjectId:15, name: 'someone', email: 'someone@mail.com'},
{ObjectId: 23, name: 'sometwo', email: 'sometwo@mail.com'},
{ObjectId: 72, name: 'seven ', email: 'seven@mail.com'},
{ObjectId: 23, name: 'five ', email: 'five@mail.com'}
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: 'sometwo@mail.com'},
{ObjectId: 23, name: 'five ', email: 'five@mail.com'}
回答1:
Yoy were close, just needed to add a new filter like this:
objetSet1 = [{id: 12, name: 'Foo Bar', email: 'foo@bar.com'},
{id: 23, name: 'Bar Foo', email: 'bar@foo.com'},
{id: 61, name: 'Barbell', email: 'barbell@mail.com'},
{id: 45, name: 'Joe Ocean', email: 'joe@ocean.com'}];
objectSet2 = [{ObjectId:15, name: 'someone', email: 'someone@mail.com'},
{ObjectId: 23, name: 'sometwo', email: 'sometwo@mail.com'},
{ObjectId: 72, name: 'seven ', email: 'seven@mail.com'},
{ObjectId: 23, name: 'five ', email: 'five@mail.com'}];
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.
回答2:
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)
回答3:
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: 'foo@bar.com'},
{id: 23, name: 'Bar Foo', email: 'bar@foo.com'},
{id: 61, name: 'Barbell', email: 'barbell@mail.com'},
{id: 45, name: 'Joe Ocean', email: 'joe@ocean.com'}
];
let objectSet2 = [
{ObjectId:15, name: 'someone', email: 'someone@mail.com'},
{ObjectId: 23, name: 'sometwo', email: 'sometwo@mail.com'},
{ObjectId: 72, name: 'seven ', email: 'seven@mail.com'},
{ObjectId: 23, name: 'five ', email: 'five@mail.com'}
];
// 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)
来源:https://stackoverflow.com/questions/60439450/typescript-how-to-get-objects-with-the-same-property-values-but-different-keys