问题描述
我有两个JavaScript对象数组,我想比较,看看它们是否相同。对象可能不(并且很可能不会)在每个数组中处于相同的顺序。每个数组不应该有超过10个对象。我认为jQuery可能有一个优雅的解决这个问题,但我不能在网上找到。
I have two arrays of JavaScript Objects that I'd like to compare to see if they are the same. The objects may not (and most likely will not) be in the same order in each array. Each array shouldn't have any more than 10 objects. I thought jQuery might have an elegant solution to this problem, but I wasn't able to find much online.
我知道一个暴躁的嵌套 $ .each(array,function(){})
解决方案可以工作,但是有没有我不知道的内置函数?
I know that a brute nested $.each(array, function(){})
solution could work, but is there any built in function that I'm not aware of?
感谢。
推荐答案
有一种简单的方法...
There is an easy way...
$(arr1).not(arr2).length === 0 && $(arr2).not(arr1).length === 0
即使元素的顺序不同,两个数组也是相同的。
If the above returns true, both the arrays are same even if the elements are in different order.
注意:这仅适用于jQuery版本< 3.0.0
NOTE: This works only for jquery versions < 3.0.0
这篇关于使用jQuery来比较两个Javascript对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!