<p>I want to compare 2 arrays of objects in JavaScript code.  The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8 items each, so maybe the brute force method of traversing each and then looking at the values of the 8 properties is the easiest way to do what I want to do, but before implementing, I wanted to see if anyone had a more elegant solution. Any thoughts?</p>

[{'answer_id': 27212, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 7, 'selected': True, 'text': '<p>EDIT: You cannot overload operators in current, common browser-based implementations of JavaScript interpreters.</p>\n\n<p>To answer the original question, one way you could do this, and mind you, this is a bit of a hack, simply <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js" rel="noreferrer">serialize the two arrays to JSON</a> and then compare the two JSON strings. That would simply tell you if the arrays are different, obviously you could do this to <em>each</em> of the objects within the arrays as well to see which ones were different.</p>\n\n<p>Another option is to use a library which has some nice facilities for comparing objects - I use and recommend <a href="http://www.mochikit.com/" rel="noreferrer"><strong>MochiKit</strong></a>.</p>\n\n<hr>\n\n<p><strong>EDIT:</strong> <a href="https://stackoverflow.com/questions/27030/comparing-arrays-of-objects-in-javascript#27932">The answer kamens gave</a> 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).</p>\n\n<p>Here is a naïve implemenation that may do just enough for you - be aware that there are potential problems with this implementation:</p>\n\n<pre><code>function objectsAreSame(x, y) {\n   var objectsAreSame = true;\n   for(var propertyName in x) {\n      if(x[propertyName] !== y[propertyName]) {\n         objectsAreSame = false;\n         break;\n      }\n   }\n   return objectsAreSame;\n}\n</code></pre>\n\n<p>The assumption is that both objects have the same exact list of properties.</p>\n\n<p>Oh, and it is probably obvious that, for better or worse, I belong to the only-one-return-point camp. :)</p>\n'}, {'answer_id': 27932, 'author': 'kamens', 'author_id': 1335, 'author_profile': 'https://Stackoverflow.com/users/1335', 'pm_score': 5, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 649465, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>The <code>objectsAreSame</code> function mentioned in @JasonBunting's answer works fine for me. However, there's a little problem: If <code>x[propertyName]</code> and <code>y[propertyName]</code> are objects (<code>typeof x[propertyName] == 'object'</code>), you'll need to call the function recursively in order to compare them.</p>\n"}, {'answer_id': 3746944, 'author': 'Yuval', 'author_id': 404861, 'author_profile': 'https://Stackoverflow.com/users/404861', 'pm_score': 4, 'selected': False, 'text': '<p>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 <code>map</code> function implementation and the object and array type checking. </p>\n\n<p>It returns a list of "diff objects", which are arrays with the diff info. It\'s very simple.</p>\n\n<p>Here it is: </p>\n\n<pre><code>// compare contents of two objects and return a list of differences\n// returns an array where each element is also an array in the form:\n// [accessor, diffType, leftValue, rightValue ]\n//\n// diffType is one of the following:\n//   value: when primitive values at that index are different\n//   undefined: when values in that index exist in one object but don\'t in \n//              another; one of the values is always undefined\n//   null: when a value in that index is null or undefined; values are\n//         expressed as boolean values, indicated wheter they were nulls\n//   type: when values in that index are of different types; values are \n//         expressed as types\n//   length: when arrays in that index are of different length; values are\n//           the lengths of the arrays\n//\n\nfunction DiffObjects(o1, o2) {\n    // choose a map() impl.\n    // you may use $.map from jQuery if you wish\n    var map = Array.prototype.map?\n        function(a) { return Array.prototype.map.apply(a, Array.prototype.slice.call(arguments, 1)); } :\n        function(a, f) { \n            var ret = new Array(a.length), value;\n            for ( var i = 0, length = a.length; i &lt; length; i++ ) \n                ret[i] = f(a[i], i);\n            return ret.concat();\n        };\n\n    // shorthand for push impl.\n    var push = Array.prototype.push;\n\n    // check for null/undefined values\n    if ((o1 == null) || (o2 == null)) {\n        if (o1 != o2)\n            return [["", "null", o1!=null, o2!=null]];\n\n        return undefined; // both null\n    }\n    // compare types\n    if ((o1.constructor != o2.constructor) ||\n        (typeof o1 != typeof o2)) {\n        return [["", "type", Object.prototype.toString.call(o1), Object.prototype.toString.call(o2) ]]; // different type\n\n    }\n\n    // compare arrays\n    if (Object.prototype.toString.call(o1) == "[object Array]") {\n        if (o1.length != o2.length) { \n            return [["", "length", o1.length, o2.length]]; // different length\n        }\n        var diff =[];\n        for (var i=0; i&lt;o1.length; i++) {\n            // per element nested diff\n            var innerDiff = DiffObjects(o1[i], o2[i]);\n            if (innerDiff) { // o1[i] != o2[i]\n                // merge diff array into parent\'s while including parent object name ([i])\n                push.apply(diff, map(innerDiff, function(o, j) { o[0]="[" + i + "]" + o[0]; return o; }));\n            }\n        }\n        // if any differences were found, return them\n        if (diff.length)\n            return diff;\n        // return nothing if arrays equal\n        return undefined;\n    }\n\n    // compare object trees\n    if (Object.prototype.toString.call(o1) == "[object Object]") {\n        var diff =[];\n        // check all props in o1\n        for (var prop in o1) {\n            // the double check in o1 is because in V8 objects remember keys set to undefined \n            if ((typeof o2[prop] == "undefined") &amp;&amp; (typeof o1[prop] != "undefined")) {\n                // prop exists in o1 but not in o2\n                diff.push(["[" + prop + "]", "undefined", o1[prop], undefined]); // prop exists in o1 but not in o2\n\n            }\n            else {\n                // per element nested diff\n                var innerDiff = DiffObjects(o1[prop], o2[prop]);\n                if (innerDiff) { // o1[prop] != o2[prop]\n                    // merge diff array into parent\'s while including parent object name ([prop])\n                    push.apply(diff, map(innerDiff, function(o, j) { o[0]="[" + prop + "]" + o[0]; return o; }));\n                }\n\n            }\n        }\n        for (var prop in o2) {\n            // the double check in o2 is because in V8 objects remember keys set to undefined \n            if ((typeof o1[prop] == "undefined") &amp;&amp; (typeof o2[prop] != "undefined")) {\n                // prop exists in o2 but not in o1\n                diff.push(["[" + prop + "]", "undefined", undefined, o2[prop]]); // prop exists in o2 but not in o1\n\n            }\n        }\n        // if any differences were found, return them\n        if (diff.length)\n            return diff;\n        // return nothing if objects equal\n        return undefined;\n    }\n    // if same type and not null or objects or arrays\n    // perform primitive value comparison\n    if (o1 != o2)\n        return [["", "value", o1, o2]];\n\n    // return nothing if values are equal\n    return undefined;\n}\n</code></pre>\n'}, {'answer_id': 6074070, 'author': 'jwood', 'author_id': 346392, 'author_profile': 'https://Stackoverflow.com/users/346392', 'pm_score': 4, 'selected': False, 'text': "<p>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 ):</p>\n\n<pre><code>function arraysAreEqual(ary1,ary2){\n  return (ary1.join('') == ary2.join(''));\n}\n</code></pre>\n"}, {'answer_id': 25237317, 'author': 'Keshav Kalra', 'author_id': 1746436, 'author_profile': 'https://Stackoverflow.com/users/1746436', 'pm_score': 1, 'selected': False, 'text': '<p>Please try this one:</p>\n\n<pre><code>function used_to_compare_two_arrays(a, b)\n{\n  // This block will make the array of indexed that array b contains a elements\n  var c = a.filter(function(value, index, obj) {\n    return b.indexOf(value) &gt; -1;\n  });\n\n  // This is used for making comparison that both have same length if no condition go wrong \n  if (c.length !== a.length) {\n    return 0;\n  } else{\n    return 1;\n  }\n}\n</code></pre>\n'}, {'answer_id': 42904039, 'author': 'Maxime Pacary', 'author_id': 488666, 'author_profile': 'https://Stackoverflow.com/users/488666', 'pm_score': 2, 'selected': False, 'text': '<p>Here is my attempt, using <a href="https://nodejs.org/api/assert.html" rel="nofollow noreferrer"><strong>Node\'s assert module</strong></a> + npm package <a href="https://www.npmjs.com/package/object-hash" rel="nofollow noreferrer"><strong>object-hash</strong></a>.</p>\n\n<p>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.</p>\n\n<pre><code>var assert = require(\'assert\');\nvar hash = require(\'object-hash\');\n\nvar obj1 = {a: 1, b: 2, c: 333},\n    obj2 = {b: 2, a: 1, c: 444},\n    obj3 = {b: "AAA", c: 555},\n    obj4 = {c: 555, b: "AAA"};\n\nvar array1 = [obj1, obj2, obj3, obj4];\nvar array2 = [obj3, obj2, obj4, obj1]; // [obj3, obj3, obj2, obj1] should work as well\n\n// calling assert.deepEquals(array1, array2) at this point FAILS (throws an AssertionError)\n// even if array1 and array2 contain the same objects in different order,\n// because array1[0].c !== array2[0].c\n\n// sort objects in arrays by their hashes, so that if the arrays are identical,\n// their objects can be compared in the same order, one by one\nvar array1 = sortArrayOnHash(array1);\nvar array2 = sortArrayOnHash(array2);\n\n// then, this should output "PASS"\ntry {\n    assert.deepEqual(array1, array2);\n    console.log("PASS");\n} catch (e) {\n    console.log("FAIL");\n    console.log(e);\n}\n\n// You could define as well something like Array.prototype.sortOnHash()...\nfunction sortArrayOnHash(array) {\n    return array.sort(function(a, b) {\n        return hash(a) &gt; hash(b);\n    });\n}\n</code></pre>\n'}, {'answer_id': 52842903, 'author': 'Sanjay Verma', 'author_id': 10515006, 'author_profile': 'https://Stackoverflow.com/users/10515006', 'pm_score': 4, 'selected': False, 'text': '<p>I tried <code>JSON.stringify()</code> and worked for me.</p>\n\n<pre><code>let array1 = [1,2,{value:\'alpha\'}] , array2 = [{value:\'alpha\'},\'music\',3,4];\n\nJSON.stringify(array1) // "[1,2,{"value":"alpha"}]"\n\nJSON.stringify(array2) // "[{"value":"alpha"},"music",3,4]"\n\nJSON.stringify(array1) === JSON.stringify(array2); // false\n</code></pre>\n'}, {'answer_id': 54031184, 'author': 'Henry Sellars', 'author_id': 9643152, 'author_profile': 'https://Stackoverflow.com/users/9643152', 'pm_score': 0, 'selected': False, 'text': '<p>using <code>_.some</code> from lodash: <a href="https://lodash.com/docs/4.17.11#some" rel="nofollow noreferrer">https://lodash.com/docs/4.17.11#some</a></p>\n\n<pre><code>const array1AndArray2NotEqual = \n          _.some(array1, (a1, idx) =&gt; a1.key1 !== array2[idx].key1 \n                                     || a1.key2 !== array2[idx].key2 \n                                     || a1.key3 !== array2[idx].key3);\n</code></pre>\n'}, {'answer_id': 55256318, 'author': 'ttulka', 'author_id': 2190498, 'author_profile': 'https://Stackoverflow.com/users/2190498', 'pm_score': 6, 'selected': False, 'text': '<p>As serialization doesn\'t work generally (only when the order of properties matches: <code>JSON.stringify({a:1,b:2}) !== JSON.stringify({b:2,a:1})</code>) you have to check the count of properties and compare each property as well:</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const objectsEqual = (o1, o2) =&gt;\r\n    Object.keys(o1).length === Object.keys(o2).length \r\n        &amp;&amp; Object.keys(o1).every(p =&gt; o1[p] === o2[p]);\r\n\r\nconst obj1 = { name: \'John\', age: 33};\r\nconst obj2 = { age: 33, name: \'John\' };\r\nconst obj3 = { name: \'John\', age: 45 };\r\n        \r\nconsole.log(objectsEqual(obj1, obj2)); // true\r\nconsole.log(objectsEqual(obj1, obj3)); // false</code></pre>\r\n</div>\r\n</div>\r\n</p>\n\n<p>If you need a deep comparison, you can call the function recursively:</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const obj1 = { name: \'John\', age: 33, info: { married: true, hobbies: [\'sport\', \'art\'] } };\r\nconst obj2 = { age: 33, name: \'John\', info: { hobbies: [\'sport\', \'art\'], married: true } };\r\nconst obj3 = { name: \'John\', age: 33 };\r\n\r\nconst objectsEqual = (o1, o2) =&gt; \r\n    typeof o1 === \'object\' &amp;&amp; Object.keys(o1).length &gt; 0 \r\n        ? Object.keys(o1).length === Object.keys(o2).length \r\n            &amp;&amp; Object.keys(o1).every(p =&gt; objectsEqual(o1[p], o2[p]))\r\n        : o1 === o2;\r\n        \r\nconsole.log(objectsEqual(obj1, obj2)); // true\r\nconsole.log(objectsEqual(obj1, obj3)); // false</code></pre>\r\n</div>\r\n</div>\r\n</p>\n\n<p>Then it\'s easy to use this function to compare objects in arrays:</p>\n\n<pre><code>const arr1 = [obj1, obj1];\nconst arr2 = [obj1, obj2];\nconst arr3 = [obj1, obj3];\n\nconst arraysEqual = (a1, a2) =&gt; \n   a1.length === a2.length &amp;&amp; a1.every((o, idx) =&gt; objectsEqual(o, a2[idx]));\n\nconsole.log(arraysEqual(arr1, arr2)); // true\nconsole.log(arraysEqual(arr1, arr3)); // false\n</code></pre>\n'}, {'answer_id': 65966136, 'author': 'Flash Noob', 'author_id': 12106367, 'author_profile': 'https://Stackoverflow.com/users/12106367', 'pm_score': -1, 'selected': False, 'text': '<p>comparing with json is pretty bad. try this package to compare nested arrays and get the difference.</p>\n<blockquote>\n<p><a href="https://www.npmjs.com/package/deep-object-diff" rel="nofollow noreferrer">https://www.npmjs.com/package/deep-object-diff</a></p>\n</blockquote>\n'}, {'answer_id': 66147209, 'author': 'Wolfram', 'author_id': 12634461, 'author_profile': 'https://Stackoverflow.com/users/12634461', 'pm_score': 3, 'selected': False, 'text': "<p>There is a optimized code for case when function needs to equals to empty arrays (and returning false in that case)</p>\n<pre><code>const objectsEqual = (o1, o2) =&gt; {\n    if (o2 === null &amp;&amp; o1 !== null) return false;\n    return o1 !== null &amp;&amp; typeof o1 === 'object' &amp;&amp; Object.keys(o1).length &gt; 0 ?\n        Object.keys(o1).length === Object.keys(o2).length &amp;&amp; \n        Object.keys(o1).every(p =&gt; objectsEqual(o1[p], o2[p]))\n        : (o1 !== null &amp;&amp; Array.isArray(o1) &amp;&amp; Array.isArray(o2) &amp;&amp; !o1.length &amp;&amp; \n        !o2.length) ? true : o1 === o2;\n}\n</code></pre>\n"}, {'answer_id': 66199056, 'author': 'Vladimir Malikov', 'author_id': 14929085, 'author_profile': 'https://Stackoverflow.com/users/14929085', 'pm_score': 0, 'selected': False, 'text': "<p>There`s my solution. It will compare arrays which also have objects and arrays. Elements can be stay in any positions.\nExample:</p>\n<pre><code>const array1 = [{a: 1}, {b: 2}, { c: 0, d: { e: 1, f: 2, } }, [1,2,3,54]];\nconst array2 = [{a: 1}, {b: 2}, { c: 0, d: { e: 1, f: 2, } }, [1,2,3,54]];\n\nconst arraysCompare = (a1, a2) =&gt; {\n  if (a1.length !== a2.length) return false;\n  const objectIteration = (object) =&gt; {\n    const result = [];\n    const objectReduce = (obj) =&gt; {\n      for (let i in obj) {\n        if (typeof obj[i] !== 'object') {\n          result.push(`${i}${obj[i]}`);\n        } else {\n          objectReduce(obj[i]);\n        }\n      }\n    };\n    objectReduce(object);\n    return result;\n  };\n  const reduceArray1 = a1.map(item =&gt; {\n    if (typeof item !== 'object') return item;\n    return objectIteration(item).join('');\n  });\n  const reduceArray2 = a2.map(item =&gt; {\n    if (typeof item !== 'object') return item;\n    return objectIteration(item).join('');\n  });\n  const compare =  reduceArray1.map(item =&gt; reduceArray2.includes(item));\n  return compare.reduce((acc, item) =&gt; acc + Number(item)) === a1.length;\n};\n\nconsole.log(arraysCompare(array1, array2));\n</code></pre>\n"}, {'answer_id': 67489928, 'author': 'Shawn W', 'author_id': 3621197, 'author_profile': 'https://Stackoverflow.com/users/3621197', 'pm_score': 2, 'selected': False, 'text': "<p>My practice implementation with sorting, tested and working.</p>\n<pre><code>const obj1 = { name: 'John', age: 33};\nconst obj2 = { age: 33, name: 'John' };\nconst obj3 = { name: 'John', age: 45 };\n\nconst equalObjs = ( obj1, obj2 ) =&gt; {\nlet keyExist = false;\nfor ( const [key, value] of Object.entries(obj1) ) {\n     // Search each key in reference object and attach a callback function to \n     // compare the two object keys\n    if( Object.keys(obj2).some( ( e ) =&gt; e == key ) ) {\n        keyExist = true;\n    }\n}\n\nreturn keyExist;\n\n}\n\n\nconsole.info( equalObjs( obj1, obj2 ) );\n</code></pre>\n<p>Compare your arrays</p>\n<pre><code>// Sort Arrays\n    var arr1 = arr1.sort(( a, b ) =&gt; {\n    var fa = Object.keys(a);\n    var fb = Object.keys(b);\n\n    if (fa &lt; fb) {\n        return -1;\n    }\n    if (fa &gt; fb) {\n        return 1;\n    }\n    return 0;\n});\n\nvar arr2 = arr2.sort(( a, b ) =&gt; {\n    var fa = Object.keys(a);\n    var fb = Object.keys(b);\n\n    if (fa &lt; fb) {\n        return -1;\n    }\n    if (fa &gt; fb) {\n        return 1;\n    }\n    return 0;\n});\n\nconst equalArrays = ( arr1, arr2 ) =&gt; {\n    // If the arrays are different length we an eliminate immediately\n    if( arr1.length !== arr2.length ) {\n        return false;\n    } else if ( arr1.every(( obj, index ) =&gt; equalObjs( obj, arr2[index] ) ) ) {\n        return true;\n      } else { \n        return false;\n      }\n    }\n\n    console.info( equalArrays( arr1, arr2 ) );\n</code></pre>\n"}, {'answer_id': 69896160, 'author': 'i6f70', 'author_id': 11617744, 'author_profile': 'https://Stackoverflow.com/users/11617744', 'pm_score': 2, 'selected': False, 'text': '<p>I am sharing my compare function implementation as it might be helpful for others:</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code> /*\n  null AND null // true\n  undefined AND undefined // true\n  null AND undefined // false\n  [] AND [] // true\n  [1, 2, \'test\'] AND [\'test\', 2, 1] // true\n  [1, 2, \'test\'] AND [\'test\', 2, 3] // false\n  [undefined, 2, \'test\'] AND [\'test\', 2, 1] // false\n  [undefined, 2, \'test\'] AND [\'test\', 2, undefined] // true\n  [[1, 2], \'test\'] AND [\'test\', [2, 1]] // true\n  [1, \'test\'] AND [\'test\', [2, 1]] // false\n  [[2, 1], \'test\'] AND [\'test\', [2, 1]] // true\n  [[2, 1], \'test\'] AND [\'test\', [2, 3]] // false\n  [[[3, 4], 2], \'test\'] AND [\'test\', [2, [3, 4]]] // true\n  [[[3, 4], 2], \'test\'] AND [\'test\', [2, [5, 4]]] // false\n  [{x: 1, y: 2}, \'test\'] AND [\'test\', {x: 1, y: 2}] // true\n  1 AND 1 // true\n  {test: 1} AND [\'test\', 2, 1] // false\n  {test: 1} AND {test: 1} // true\n  {test: 1} AND {test: 2} // false\n  {test: [1, 2]} AND {test: [1, 2]} // true\n  {test: [1, 2]} AND {test: [1]} // false\n  {test: [1, 2], x: 1} AND {test: [1, 2], x: 2} // false\n  {test: [1, { z: 5 }], x: 1} AND {x: 1, test: [1, { z: 5}]} // true\n  {test: [1, { z: 5 }], x: 1} AND {x: 1, test: [1, { z: 6}]} // false\n   */\n  function is_equal(x, y) {\n      const\n          arr1 = x,\n          arr2 = y,\n          is_objects_equal = function (obj_x, obj_y) {\n              if (!(\n                  typeof obj_x === \'object\' &amp;&amp;\n                  Object.keys(obj_x).length &gt; 0\n              ))\n                  return obj_x === obj_y;\n\n              return Object.keys(obj_x).length === Object.keys(obj_y).length &amp;&amp;\n                  Object.keys(obj_x).every(p =&gt; is_objects_equal(obj_x[p], obj_y[p]));\n          }\n          ;\n\n      if (!( Array.isArray(arr1) &amp;&amp; Array.isArray(arr2) ))\n          return (\n              arr1 &amp;&amp; typeof arr1 === \'object\' &amp;&amp;\n              arr2 &amp;&amp; typeof arr2 === \'object\'\n          )\n              ? is_objects_equal(arr1, arr2)\n              : arr1 === arr2;\n\n      if (arr1.length !== arr2.length)\n          return false;\n\n      for (const idx_1 of arr1.keys())\n          for (const idx_2 of arr2.keys())\n              if (\n                  (\n                      Array.isArray(arr1[idx_1]) &amp;&amp;\n                      this.is_equal(arr1[idx_1], arr2[idx_2])\n                  ) ||\n                  is_objects_equal(arr1[idx_1], arr2[idx_2])\n              )\n              {\n                  arr2.splice(idx_2, 1);\n                  break;\n              }\n\n      return !arr2.length;\n  }</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 70286558, 'author': 'Mr.P', 'author_id': 3257387, 'author_profile': 'https://Stackoverflow.com/users/3257387', 'pm_score': 1, 'selected': False, 'text': '<p>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 ....</p>\n<p>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</p>\n<p>there is absolutely a way for optimization but it\'s working :)</p>\n<p>thx to <a href="https://stackoverflow.com/a/55256318/3257387">@ttulka</a> I got inspired by his work ... just worked on it a little bit</p>\n<pre><code>const objectsEqual = (o1, o2) =&gt; {\n  let match = false\n    if(typeof o1 === \'object\' &amp;&amp; Object.keys(o1).length &gt; 0) {\n     match = (Object.keys(o1).length === Object.keys(o2).length &amp;&amp; Object.keys(o1).every(p =&gt; objectsEqual(o1[p], o2[p])))\n    }else {\n     match = (o1 === o2)\n    }\n    return match\n}\n\nconst arraysEqual = (a1, a2) =&gt; {\n  let finalMatch = []\n  let itemFound = []\n  \n  if(a1.length === a2.length) {\n    finalMatch = []\n    a1.forEach( i1 =&gt; {\n      itemFound = []\n      a2.forEach( i2 =&gt; { \n        itemFound.push(objectsEqual(i1, i2)) \n      })\n        finalMatch.push(itemFound.some( i =&gt; i === true))  \n    }) \n  } \n  return finalMatch.every(i =&gt; i === true)\n}\n\nconst ar1 = [\n  { id: 1, name: &quot;Johnny&quot;, data: { body: &quot;Some text&quot;}},\n  { id: 2, name: &quot;Jimmy&quot;}\n]\nconst ar2 = [\n  {name: &quot;Jimmy&quot;, id: 2},\n  {name: &quot;Johnny&quot;, data: { body: &quot;Some text&quot;}, id: 1}\n]\n\n\nconsole.log(&quot;Match:&quot;,arraysEqual(ar1, ar2))\n</code></pre>\n<p>jsfiddle: <a href="https://jsfiddle.net/x1pubs6q/" rel="nofollow noreferrer">https://jsfiddle.net/x1pubs6q/</a></p>\n<p>or just use lodash :))))</p>\n<pre><code>const _ = require(\'lodash\')\n\nconst isArrayEqual = (x, y) =&gt; {\n  return _.isEmpty(_.xorWith(x, y, _.isEqual));\n};\n</code></pre>\n'}, {'answer_id': 72691567, 'author': 'Oussama Filani', 'author_id': 9012478, 'author_profile': 'https://Stackoverflow.com/users/9012478', 'pm_score': 0, 'selected': False, 'text': '<p>This is work for me to compare two array of objects without taking into consideration the order of the items</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const collection1 =  [\n  { id: "1", name: "item 1", subtitle: "This is a subtitle", parentId: "1" },\n  { id: "2", name: "item 2", parentId: "1" },\n  { id: "3", name: "item 3", parentId: "1" },\n]\nconst collection2 =  [\n  { id: "3", name: "item 3", parentId: "1" },\n  { id: "2", name: "item 2", parentId: "1" },\n  { id: "1", name: "item 1", subtitle: "This is a subtitle", parentId: "1" },\n]\n\n\nconst contains = (arr, obj) =&gt; {\n  let i = arr.length;\n  while (i--) {\n     if (JSON.stringify(arr[i]) === JSON.stringify(obj)) {\n         return true;\n     }\n  }\n  return false;\n}\n\nconst isEqual = (obj1, obj2) =&gt; {\n  let n = 0\n  if (obj1.length !== obj2.length) {\n      return false;\n  }\n  for (let i = 0; i &lt; obj1.length; i++) {\n      if (contains(obj2, obj1[i])) {\n        n++\n      }\n  }\n  return n === obj1.length\n}\n\nconsole.log(isEqual(collection1,collection2))</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<p>if you take into consideration the order of the items use built in function in lodash <a href="https://docs-lodash.com/v4/is-equal/" rel="nofollow noreferrer">isEqual</a></p>\n'}, {'answer_id': 72846327, 'author': 'C-lio Garcia', 'author_id': 2638849, 'author_profile': 'https://Stackoverflow.com/users/2638849', 'pm_score': -1, 'selected': False, 'text': "<p>If you stringify them...</p>\n<pre><code>type AB = {\n  nome: string;\n}\n\nconst a: AB[] = [{ nome: 'Célio' }];\nconst b: AB[] = [{ nome: 'Célio' }];\n\nconsole.log(a === b); // false\nconsole.log(JSON.stringify(a) === JSON.stringify(b)); // true\n\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/27030', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2176/']