检查两个数组是否具有具有同一属性的对象
这是我的目标:比较两个对象,并找出是否有1个或更多项目。如果有1个或更多共同点,请返回 true
否则(没有共同的项目),返回 false
。
当前问题:我尝试使用 .some()
方法,其中1个来自API的对象和1个本地对象,但有些困惑,为什么它不起作用。 ..任何想法?它应该返回true,因为John都在两个对象中,但是它返回false false
代码示例:在此示例中,它应该返回 true
,因为John是两个都是对象的名称1(结果1)和对象2(结果2)。但是,它返回 false
。
有人能帮助我了解我在这里做错了什么吗?
var result1 = [
{id:1, name:'Sandra', type:'user', username:'sandra'},
{id:2, name:'John', type:'admin', username:'johnny2'},
{id:3, name:'Peter', type:'user', username:'pete'},
{id:4, name:'Bobby', type:'user', username:'be_bob'}
];
var result2 = [
{id:2, name:'John', email:'[email protected]'},
{id:4, name:'Bobby', email:'[email protected]'}
];
const hasSimilarElement = result1.some((item) => item.name === result2.name);
console.log(hasSimilarElement);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以创建一个函数,该函数将功能
f
用于查找,然后两个数组xs
&ys
:然后::
You can create a function that takes a function
f
to use for the lookup then two arraysxs
&ys
:Then:
我将使用CustomCommander提出的技术来解决您的确切情况,并且在任何地方,您都可以从对象中提取一个唯一的键作为原始类型。 (这里
name
是字符串,因此有效。)如果您需要更通用的东西,则可以提供一个函数,该函数告诉两个元素是否相等并这样使用:
在您可以生成唯一键的情况下,这效率较低,但更通用。您甚至可以将其用于两个数组,其中两个数组具有具有不同结构的对象,例如
(x,y)=> x .id == y .primarykey
。I would use the technique proposed by customcommander for your exact scenario, and for anywhere you can extract a unique key from your objects as a primitive type. (Here
name
s are strings, so that works.)If you need something more generic, then you can supply a function that tells if two elements are equal and use it like this:
This is less efficient in those cases where you could generate a unique key, but it's more generic. You can even use it for two arrays where the two arrays have objects with different structures, passing for instance
(x, y) => x .id == y .primaryKey
.在您的示例中,您正在尝试比较2个对象。您的结果为false2.名称不确定,您可能需要指定特定元素的索引,例如结果2 [0]
。您可以这样使用以下方式:
In your example you are trying to compare 2 arrays of objects. You have false as result2.name is undefined, you might need to specify the index of an particular element e.g. result2[0].name
If you'd like to compare 2 arrays with some you need to iterate through result2 items as well. You can use somehting like this:
您的代码失败的原因是,您正在尝试在Result1数组的元素中找到
name
result2.name,但是
result2
也是一个array
,数组没有.name
,恰好是所有元素.name
s,以某种方式与一个匹配您正在寻找 - 这不是它的工作方式,您需要迭代两个阵列才能寻找匹配
但是,对于非常大的结果集,这是效率低下的 - 我不是“大符号专家,但我相信这将是O(N 2 ) - 您可能会比较
> result1.length
*result.2.length
对于此简单情况,您可以从两个结果中提取
.name
/code> ...如果set
大小小于组合结果.length
s,则表示这里有一个副本,您将每个结果迭代一次 - 如果这两套都有1000个元素,为2,000次迭代,而使用上面的幼稚方法为1,000,000
但是,有一个幻影在其中一个结果中有一个重复的名称,这将无法按照预期的那样起作用
,每组迭代两次 - 但是,鉴于2 x 1,000长的结果,4,000次迭代仍然好于1,000,000
The reason your code fails is that you are trying to find a match for a
name
in the elements of result1 Array toresult2.name
However,
result2
is also anArray
, Array's do not have a.name
that happens to be all of the elements.name
s that somehow matches the one you are searching for - that's not how it worksYou need to iterate both Arrays to look for a match
However, for very large result sets this is inefficient - I'm no "big O notation expert, but I believe this would be O(n2) - you're potentially going to compare
result1.length
*result2.length
timesFor this simple case, you can extract just the
.name
from both results, and create aSet
... if theSet
size is less than the combined result.length
s it means there's a duplicatehere, you'll iterate each result once each - if both sets have 1000 elements that's 2,000 iterations, compared to 1,000,000 using the naive method above
However, there is a flawif there's a duplicate name within one of the results, this will not work as expected
Here each set is iterated twice - but, given 2 x 1,000 long results, 4,000 iterations is still better than 1,000,000
从上面的评论...
。可以交换 noreflow noreferrer“> 对于另一个 再次。
在下一个迭代中,原因可能会带来一个普遍实现的功能,该功能允许可以自由选择属性名称。
实现函数还使摆脱两个嵌套的迭代(尽管
某些原因
提前退出以进行成功比较)。这里将从较短的数组构建
键
(属性名称)特定值索引(单个迭代任务),其中,在迭代较长的数组时,会查找(否迭代)<<代码>键项目相似性的特定值。From the above comment ...
Or as correctly suggested by 3limin4t0r one could swap
find
for anothersome
again.Within a next iteration one of cause could come up with a generically implemented function which allows the to be compared property name to be freely chosen.
Implementing a function also gives the opportunity to get rid of the two nested
some
iterations (thoughsome
of cause exits early for a successful comparison).Here one would build from the shorter array a
key
(property name) specific value-index (a single iteration task), where, while iterating the longer array, one would look up (no iteration) thekey
specific value for item similarity.