How to know if two arrays have the same values

javascript check if two arrays have same elements
how to check if two arrays are equal javascript
how to check if two arrays are equal c++
javascript compare two arrays for matches
javascript compare two arrays of objects
check if two arrays contain same elements c#
compare two arrays javascript
comparing two arrays in javascript returning differences

I have these two arrays: one is filled with information from an ajax request and another stores the buttons the user clicks on. I use this code (I filled with sample numbers):

var array1 = [2, 4];
var array2 = [4, 2]; //It cames from the user button clicks, so it might be disordered.
array1.sort(); //Sorts both Ajax and user info.
if (array1==array2) {

But it always gives false, even if the two arrays are the same, but with different name. (I checked this in Chrome's JS Console). So, is there any way I could know if these two arrays contain the same? Why is it giving false? How can I know which values in the first array are not in the second?

function arraysEqual(_arr1, _arr2) {

    if (!Array.isArray(_arr1) || ! Array.isArray(_arr2) || _arr1.length !== _arr2.length)
      return false;

    var arr1 = _arr1.concat().sort();
    var arr2 = _arr2.concat().sort();

    for (var i = 0; i < arr1.length; i++) {

        if (arr1[i] !== arr2[i])
            return false;


    return true;


Note that this doesn't modify original arrays unlike a previous answer.

Check if two arrays are equal or not, How to convert an Object {} to an Array [] of key-value pairs in JavaScript? Index inside map() Function · How to pass an array as a function parameter in JavaScript  For non-primitive data types like arrays, == and === check for reference equality. That is, they check whether arr1 and arr2 are the same object. In your example, the two arrays have the same objects in the same order, but are not equivalent.

If your array items are not objects- if they are numbers or strings, for example, you can compare their joined strings to see if they have the same members in any order-

var array1= [10, 6, 19, 16, 14, 15, 2, 9, 5, 3, 4, 13, 8, 7, 1, 12, 18, 11, 20, 17];
var array2= [12, 18, 20, 11, 19, 14, 6, 7, 8, 16, 9, 3, 1, 13, 5, 4, 15, 10, 2, 17];

if(array1.sort().join(',')=== array2.sort().join(',')){
    alert('same members');
else alert('not a match');

How to find if two arrays contain any common item in Javascript , function). That they have the same value. And if the item is itself an array or object​, we need to compare all of its values  Doesn't this just test if the two arrays are actually different names for the same object, rather than two different objects that have the same values? – acdr May 15 '17 at 9:39 Not as far as I can tell.

If you want to check only if two arrays have same values (regardless the number of occurrences and order of each value) you could do this by using lodash:

_.isEmpty(_.xor(array1, array2))

Short, simple and pretty!

Check if two arrays or objects are equal with JavaScript, guides) asked me what the best way to check if two arrays are equal is. different even if the two objects have all the same keys and values. probably the easiest way is to sort both arrays then all you have to do is check then from top to bottom, if any of the elements in array A are not the same as the corresponding element in array B then they are different. = function(testArr) {
    if (this.length != testArr.length) return false;
    for (var i = 0; i < testArr.length; i++) {
        if (this[i].compare) { //To test values in nested arrays
            if (!this[i].compare(testArr[i])) return false;
        else if (this[i] !== testArr[i]) return false;
    return true;

var array1 = [2, 4];
var array2 = [4, 2];
if(array1.sort().compare(array2.sort())) {
} else {


Checking if two arrays are equal, A couple of things about your implementation: indexOf will return the index of the first matching element. If there are duplicate elements it will  Check if two arrays are equal or not Given two given arrays of equal length, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though.

Check if two arrays have equal elements in Javascript, The only way to check equality of the contents of two arrays is to iterate through them and check the values that they contain. Since your arrays  How to check if two arrays are equal or not? Following example shows how to use equals () method of Arrays to check if two arrays are equal or not. The above code sample will produce the following result. The above code sample will produce the following result. The above code sample will produce the following result.

Is this the only way to check whether two arrays are identical , Step 1: We need to declare the function with two parameters. function compare(​arr1 If something is true we started entering into the if block. Because we are using repeated elements in the both arrays check out arr1 and arr2. To get the  The function you wrote says the two arrays are similar because you're simply checking if each of the elements in the first array exist in the second array. In the test example you've put here, it is the case so you're getting 1 as a return.

Comparison of Two arrays Using JavaScript, For two arrays to be equal, they must have the same number of elements. If it returns 0 (false) then you know that the value exists somewhere within the array. How to compare array contents? A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. Actually, there is a list of equals() methods in Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is base of all classes in Java).

ARRAY_EQUAL, Example. Compare the values of two arrays, and return the matches: <?php $a1=​array  If they are a perfect match, there would be 0 missing values in each column. (I decided to do missing values instead of matching values so that I wouldn't have to know HOW MANY values were in the range to begin with.) My array formula for A7 = SUM(IF(ISNA(MATCH(A2:A6, B2:B6, 0)), 1, 0)), Ctrl + Shift + Enter.

  • I am pretty certain you need to go through each element of the arrays.
  • Do you know why it returns false? Curious.
  • See @Andrew 's answer
  • possible duplicate of how to check javascript array equals?
  • sorting takes nlog(n) time. You don't need sorting. This answer works in linear time.
  • This will work well for primitives or objects that have uniquely identifying toString values, but not for just any objects.
  • Thanks! neat solution
  • Beware of null items and sorting. I ended up in my case with strings to compare like ",2,2,3" and "2,2,3," which of course aren't strictly equal.
  • Could fail for strings, i.e. ['a', 'b'] and ['a,b']. I would only recommend this technique for small throwaway scripts.
  • Hi kennebec, can you tell how to save matched into another array
  • I cannot seem to find xor in the underscore docs? Are you thinking of IODash?
  • Thank you! It works just as desired. I modified a little the function so I could also know how many mismatches are.
  • false for [2,4] [4,2].
  • @SurazKhanal Still need to sort