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
2

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I need to compare two arrays with different positions.
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);

Comments

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

Comments

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');
         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.