我有两个JavaScript对象数组,我想比较它们是否相同.对象在每个数组中的顺序可能不相同(很可能也不会).每个数组不应包含超过10个对象.我认为jQuery可能有一个优雅的解决方案,但我在网上找不到太多.
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?
Thanks.
我有两个JavaScript对象数组,我想比较它们是否相同.对象在每个数组中的顺序可能不相同(很可能也不会).每个数组不应包含超过10个对象.我认为jQuery可能有一个优雅的解决方案,但我在网上找不到太多.
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?
Thanks.
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.
NOTE: This works only for jquery versions < 3.0.0 when using JSON objects