Skip to content

Compare Array in JavaScript | Find difference, Equals or not Exampels

  • by

How to compare elements in an array of JavaScript?

To compare arrays, loop through them and compare every value. In the example compare two arrays… and result true if they are identical, and false if not.

Example of Compare Array in JavaScript (array.equals)

<!DOCTYPE html>
<html>

<body>
    <script type="text/javascript">

        // Warn if overriding existing method
        if(Array.prototype.equals)
            console.warn("Overriding existing Array.prototype.equals. ----");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time 
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i] != array[i]) { 
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;   
        }           
    }       
    return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});

// Testing 
console.log([1, 2, [3, 4]].equals([1, 2, [3, 2]]));

console.log([1, "2,3"].equals([1, 2, 3]));

console.log([1, 2, [3, 4]].equals([1, 2, [3, 4]]));

console.log([1, 2, 1, 2].equals([1, 2, 1, 2]));


</script>

</body>
</html>

Source: stackoverflow

Output:

Compare Array in JavaScript array.equals

Q: How to get the difference between two arrays in JavaScript?

Answer:- Comparing a normal array is easy. You need to use the a for .. in loop.

Here is a example code to return the difference between two arrays in JavaScript:-

<!DOCTYPE html>
<html>

<body>
    <script type="text/javascript">
        var a1 = ['a', 'b'];
        var a2 = ['a', 'b', 'c', 'd'];

        function arr_diff (a1, a2) {

            var a = [], diff = [];

            for (var i = 0; i < a1.length; i++) {
                a[a1[i]] = true;
            }

            for (var i = 0; i < a2.length; i++) {
                if (a[a2[i]]) {
                    delete a[a2[i]];
                } else {
                    a[a2[i]] = true;
                }
            }

            for (var k in a) {
                diff.push(k);
            }

            return diff;
        }

        console.log(arr_diff(a1,a2));
        
    </script>

</body>
</html>

Output: Array [ “c”, “d” ]

Note: If you don’t care about backward compatibility, is using filter.

Q: How to Compare two Arrays are Equal using JavaScript?

Answer: You could use Array.prototype.every().

var array1 = [4,8,9,10];
var array2 = [4,8,9,10];

var is_same = array1.length == array2.length && array1.every(function(element, index) {
    return element === array2[index]; 
});

console.log(is_same);

Do comment if you know better example or any doubts or suggestions on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading