Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
63
rated 0 times [  67] [ 4]  / answers: 1 / hits: 134084  / 15 Years ago, fri, november 20, 2009, 12:00:00

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.



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.


More From » jquery

 Answers
7

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


[#98262] Wednesday, November 18, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
santana

Total Points: 595
Total Questions: 94
Total Answers: 98

Location: Zambia
Member since Mon, Oct 24, 2022
2 Years ago
;