1

I have a simple application in which I want compare two arrays.

function check() {
    var Array1 = value.length;
    var Array2 = selected_value.length;
    alert(Array1 + "," + Array2);
    for (var i = 0; i < Array1; i++) {
        for (var j = 0; j < Array2; j++) {
            if (value[i] == selected_value[j]) {
                alert("both div are same");
            }
        }
    }
}​

my arrays have 4 elements each but the if condition is not satisfied.

Starx
  • 77,474
  • 47
  • 185
  • 261
Pranali
  • 105
  • 3
  • 13
  • 4
    In future, please consider spending more time formatting your question. Making it look like you've spent more than 4 seconds writing your question through correct indentation and proper spelling will increase your chances of getting an answer. – Matt Apr 17 '12 at 10:02
  • Think of an algorithm to do the task. What you have checks for each (sic!) combination of values whether they are the same, and for each equal pair it alerts "both div are same". – Bergi Apr 17 '12 at 10:03
  • Your code works: http://jsfiddle.net/BBedy/ – Florian Margaine Apr 17 '12 at 10:03
  • actually my one array contain value which I retrive from jsonobject but I stored whole value inside array – Pranali Apr 17 '12 at 10:07
  • possible duplicate of [How to check identical array in most efficient way?](http://stackoverflow.com/questions/4025893/how-to-check-identical-array-in-most-efficient-way) – Starx Apr 17 '12 at 10:07

2 Answers2

0

try to view yout values. before if write console.log( value[i], selected_valuw[j]) and check whether they are alike

Michael Sazonov
  • 1,531
  • 11
  • 21
-2

Here is function, with which you can compare two arrays

function arraysEqual(arr1, arr2) {
    if(arr1.length !== arr2.length)
        return false;
    for(var i = arr1.length; i--;) {
        if(arr1[i] !== arr2[i])
            return false;
    }

    return true;
}

Source

You can check if array return false, like this

if(!arrayEqual(arr1,arr2)) {
   // false
}
Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261