Compare 2 array object javascript
As serialization doesn't work generally (only when the order of properties matches: Show
If you need a deep comparison, you can call the function recursively:
Then it's easy to use this function to compare objects in arrays:
answered Mar 20, 2019 at 8:22
ttulkattulka 9,1134 gold badges37 silver badges48 bronze badges 11 EDIT: You cannot overload operators in current, common browser-based implementations of JavaScript interpreters. To answer the original question, one way you could do this, and mind you, this is a bit of a hack, simply serialize the two arrays to JSON and then compare the two JSON strings. That would simply tell you if the arrays are different, obviously you could do this to each of the objects within the arrays as well to see which ones were different. Another option is to use a library which has some nice facilities for comparing objects - I use and recommend MochiKit. EDIT: The answer kamens gave deserves consideration as well, since a single function to compare two given objects would be much smaller than any library to do what I suggest (although my suggestion would certainly work well enough). Here is a naïve implemenation that may do just enough for you - be aware that there are potential problems with this implementation:
The assumption is that both objects have the same exact list of properties. Oh, and it is probably obvious that, for better or worse, I belong to the only-one-return-point camp. :)
answered Aug 26, 2008 at 1:39
Jason BuntingJason Bunting 57.2k14 gold badges99 silver badges93 bronze badges 4 Honestly, with 8 objects max and 8 properties max per object, your best bet is to just traverse each object and make the comparisons directly. It'll be fast and it'll be easy. If you're going to be using these types of comparisons often, then I agree with Jason about JSON serialization...but otherwise there's no need to slow down your app with a new library or JSON serialization code. answered Aug 26, 2008 at 12:52
kamenskamens 11.6k6 gold badges44 silver badges46 bronze badges 1 I know this is an old question and the answers provided work fine ... but this is a bit shorter and doesn't require any additional libraries ( i.e. JSON ):
Mr.Wizard 24k5 gold badges42 silver badges120 bronze badges answered May 20, 2011 at 15:23
jwoodjwood 3472 silver badges6 bronze badges 8 I have worked a bit on a simple algorithm to compare contents of two objects and return an
intelligible list of difference. Thought I would share. It borrows some ideas for jQuery, namely the It returns a list of "diff objects", which are arrays with the diff info. It's very simple. Here it is:
answered Sep 19, 2010 at 18:32
YuvalYuval 3,02629 silver badges41 bronze badges I tried
Paul Roub 36k27 gold badges80 silver badges88 bronze badges answered Oct 16, 2018 at 19:38
3 There is a optimized code for case when function needs to equals to empty arrays (and returning false in that case)
answered Feb 11, 2021 at 0:39
WolframWolfram 411 silver badge3 bronze badges 1 Here is my attempt, using Node's assert module + npm package object-hash. I suppose that you would like to check if two arrays contain the same objects, even if those objects are ordered differently between the two arrays.
answered Mar 20, 2017 at 12:48
Frosty ZFrosty Z 21.6k11 gold badges81 silver badges110 bronze badges My practice implementation with sorting, tested and working.
Compare your arrays
answered May 11, 2021 at 15:33
Shawn WShawn W 5164 silver badges13 bronze badges Please try this one:
c0deNinja 3,9201 gold badge28 silver badges45 bronze badges answered Aug 11, 2014 at 6:51
1 The
answered Mar 16, 2009 at 6:40 I am sharing my compare function implementation as it might be helpful for others:
Ruli 2,44112 gold badges27 silver badges35 bronze badges answered Nov 9, 2021 at 10:01
0 using
adiga 32.2k8 gold badges55 silver badges78 bronze badges answered Jan 3, 2019 at 23:24
0 There`s my solution. It will compare arrays which also have objects and arrays. Elements can be stay in any positions. Example:
answered Feb 14, 2021 at 19:07
not sure about the performance ... will have to test on big objects .. however, this works great for me.. the advantage it has compared to the other solutions is, the objects/array do not have to be in the same order .... it practically takes the first object in the first array, and scans the second array for every objects .. if it's a match, it will proceed to another there is absolutely a way for optimization but it's working :) thx to @ttulka I got inspired by his work ... just worked on it a little bit
jsfiddle: https://jsfiddle.net/x1pubs6q/ or just use lodash :))))
answered Dec 9, 2021 at 8:14
Mr.PMr.P 1,0591 gold badge17 silver badges38 bronze badges This is work for me to compare two array of objects without taking into consideration the order of the items
if you take into consideration the order of the items use built in function in lodash isEqual answered Jun 20 at 18:36
2 If you stringify them...
answered Jul 3 at 12:01
1 How do you compare two objects in an array?To properly compare two arrays or objects, we need to check:. That they're the same object type (array vs. object).. That they have the same number of items.. That each item is equal to its counterpart in the other array or object. That they're the same object type (array vs. object vs. string vs. number vs. function).. How do I compare two arrays of objects in es6?“compare two array of objects javascript es6” Code Answer's. var result = result1. filter(function (o1) {. return result2. some(function (o2) {. return o1. id === o2. id; // return the ones with equal id.. // if you want to be more clever.... let result = result1. filter(o1 => result2. some(o2 => o1. id === o2. id));. How do I compare two arrays in JavaScript?Find difference between two arrays in JavaScript. Using Array.prototype.filter() function. You can use the filter() method to find the elements of the first array which are not in the second array. ... . Using jQuery. With jQuery, you can use the .not() method to get the difference. ... . Using Underscore/Lodash Library.. Can you use == to compare arrays?equals() to compare two arrays is often misconstrued as content equality, and because a better alternative exists in the use of reference equality operators, the use of the Object. equals() method to compare two arrays is disallowed.
|