2021年1月25日星期一

How to get all properties of the second array of objects when comparing with the first?

I am using JavaScript with lodash and have two array of objects as follows:

objArr1 = [{'x': 1, 'y': 2, 'z': 3}, {'x': 10, 'y': 20, 'z': 30}, {'x': 100, 'y': 200, 'z': 300}, {'x': 1000, 'y': 2000, 'z': 3000}, {'x': 10000, 'y': 20000, 'z': 30000}]    objArr2 = [{'x': 1, 'y': 2, 'a': 5}, {'x': 10, 'y': 20, 'a': 6}, {'x': 100, 'y': 200, 'a': 9}]  

I am using the matching key as 'x' and I am able to get:

_.intersectionBy(objArr1, objArr2, 'x')    [{'x': 1, 'y': 2, 'z': 3}, {'x': 10, 'y': 20, 'z': 30}, {'x': 100, 'y': 200, 'z': 300}]  

However, I also need to get the 'a' property in the second array of objects.

e.g.

[{'x': 1, 'y': 2, 'z': 3, 'a': 5}, {'x': 10, 'y': 20, 'z': 30, 'a': 6}, {'x': 100, 'y': 200, 'z': 300, 'a': 9}]  

Couldn't seem to figure out using lodash.

Please help! Thanks!

https://stackoverflow.com/questions/65894808/how-to-get-all-properties-of-the-second-array-of-objects-when-comparing-with-the January 26, 2021 at 09:40AM

没有评论:

发表评论