0

I want to check two array values are same or not. I am using a form with checkboxes. need show any change in checkbox array or not?. can anyone help me. Two arrays Like this.

array1 = ['1','2','3']; //previous checklist
array2 = ['3','2','1']; //new checklist
Dayachand Patel
  • 469
  • 4
  • 12
  • Please, read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and show us what you have tried. So we can start from that to discuss you issue. – Andrea Mar 10 '17 at 09:46
  • 2
    Possible duplicate of [How to compare arrays in JavaScript?](http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Sachila Ranawaka Mar 10 '17 at 09:47

4 Answers4

1

Here is a snippet that compares two arrays.

var array1 = [1,2,3];
var array2 = [1,2,3];

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

alert(result);

however 1,2,3 in one array is not equal with 3,2,1 in another. You didn't mentioned about to check the array elements or just the array !

In Case you need to compare two arrays with different positions, try this

var array1=[1,2,3,4]
var array2=[1,4,3,2]
var result = array1.length==array2.length && array1.every(function(v,i) { return ($.inArray(v,array2) != -1)})
console.log(result)
Ranjith Varatharajan
  • 1,596
  • 1
  • 33
  • 76
0

I got this:

let arr = ['1', '2', '3'];
let arr2 = ['3', '1', '2'];

let finalResult = arr.length === arr2.length;
arr.forEach(function (item) {
  if (!finalResult) {
    return;
  }

  if (arr2.find(function (item2) { return item === item2; }) === undefined) {
    finalResult = false;
  }
});

console.log(finalResult);
Callum Linington
  • 14,213
  • 12
  • 75
  • 154
0
// Warn if overriding existing method

if(Array.prototype.equals)
    console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");

// 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++) {
        // Checking whether the array contains this element
        if(isValueExistsInArray(this[i],array) == false) {
            return false;
        }       
    }       
    return true;
}
function isValueExistsInArray(value,compareToArray) {
    for(var j = 0, k=compareToArray.length; j<k; j++) {
        if(value == compareToArray[j]) {
            return true;
        }
    }
    return false;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});

array1 = ['1','2','3'];
array2 = ['1','2','3'];
array3 = ['3','2','1'];
array4 = ['3','5','1'];
array5 = ['3','5','1',6];

array1.equals(array2) == true
array1.equals(array3) == true
array1.equals(array4) == false
array1.equals(array5) == false
Bhanu Boppana
  • 134
  • 1
  • 2
  • 10
0

Finally I have perfect answer for compare two array in javascript.

var array1 = ["1","2","3"];

        var arr1 = array1.map(function (x) {
            return parseInt(x, 10);
        });
        var array2 = ["3","2","1"];
        var arr2 = array2.map(function (x) {
            return parseInt(x, 10);
        });

        var finalArray1 = arr1.sort();
        var finalArray2 = arr2.sort();

        var is_same = finalArray1.length == finalArray2.length && finalArray1.every(function(element, index) {
                return element === finalArray2[index];
            });
         if(is_same == true){
            console.log('match');
         }else{
            console.log('not match');
         }
Dayachand Patel
  • 469
  • 4
  • 12