通过动态键在两个对象之间查找相交
我有两个带有动态键的对象数组(我永远不知道钥匙的名称); 示例:
hierarchy1: [
{
level1: 'Shoes',
}
]
hierarchy2: [
{
level1: 'Shoes',
level2: 'Sneakers',
},
]
我需要找到层次结构和层次结构之间的相交。我不能使用lodash _.Intersectionby,因为我不知道我会在 eReSarchy1 中获得的密钥的名称。
我希望这样的结果[{latve1:'shoes'}] 有什么想法如何解决这个问题?
多谢!
I have 2 arrays of objects with dynamic keys (i never know the name of the key);
Example:
hierarchy1: [
{
level1: 'Shoes',
}
]
hierarchy2: [
{
level1: 'Shoes',
level2: 'Sneakers',
},
]
I need to find intersection between hierarchy1 and hierarchy2. I can't use lodash _.intersectionBy because i dont know the name of the key i will get in hierarchy1.
I'd expect to get the result like this [{ level1: 'Shoes' }]
Any ideas how to solve this issue?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想与每个索引进行比较,那么您可以做这样的事情
对第一种方法的一种改进是进行一些预估计,您可以简单地将特定键的所有值编成,并且在循环时可以检查给定键是否存在特定值
如果您只想比较各自的索引或两个数组,则可以做这样的事情
if you want to compare with every index then you can do something like this
an improvement to first approach is doing some pre-calculations, you can simply club all the values for a particular key and when looping over you can check if there's particular value present for the given key or not
If you just want to compare respective indexes or both array you can do something like this
您可以使用它来重塑一下。
然后将其放入您想要的任何格式中
You can use this to reshape it a tad.
Then put it into whatever format you want after that