1

Comparing a array with another array values. I have an array with a values below.

a = [1,5,6,7,3,8,12];

Below is the array which i to need to check.

b = [3,5,7];

I have to check whether a contains the value of b.

function contains(n){
  for(var i=0; i<this.length; i++){
         if(this[i] == n)
             return true;
  }
  return false;
}

a.contains("12"); //would work
a.contains(b) //check for an array values?

The above code works fine when i pass in a number, can anyone tell me how would i pass an array and then efficiently compare with another array.

UnderTaker
  • 853
  • 3
  • 13
  • 22

3 Answers3

2
  1. You should extend your array inorder to use that function.
  2. in this.length; it does not refers to function, not the array

Try the code below.

Array.prototype.equals = function (array, strict) {
    if (!array)
        return false;

    if (arguments.length == 1)
        strict = true;

    if (this.length != array.length)
        return false;

    for (var i = 0; i < this.length; i++) {
        if (this[i] instanceof Array && array[i] instanceof Array) {
            if (!this[i].equals(array[i], strict))
                return false;
        }
        else if (strict && this[i] != array[i]) {
            return false;
        }
        else if (!strict) {
            return this.sort().equals(array.sort(), true);
        }
    }
    return true;
}



var arr1 = [1, 2, 3, 4];
var arr2 = [2, 1, 4, 3];  // Loosely equal to 1
var arr3 = [2, 2, 3, 4];  // Not equal to 1
var arr4 = [1, 2, 3, 4];  // Strictly equal to 1

arr1.equals(arr2);         // false
arr1.equals(arr2, false);  // true
arr1.equals(arr3);         // false
arr1.equals(arr3, false);  // false
arr1.equals(arr4);         // true
arr1.equals(arr4, false);  // true

I hope it helps

Ashok Shah
  • 956
  • 12
  • 39
1

I assume your contains function is on Array.prototype (because of your use of this).

Given that, you can just use .every() to iterate b and test every value.

function contains(n){
  for(var i=0; i<this.length; i++){
         if(this[i] == n)
             return true;
  }
  return false;
}

Array.prototype.contains = contains;

var a = [1,5,6,7,3,8,12];

var b = [3,5,7];

var has_all = b.every(function(val) {
    return a.contains(val);
});

alert(has_all);

The .every() method will short circuit on the first value that fails and will return true if all were found, otherwise false.


ECMScript 5 has an .indexOf() method, so you could actually use that instead of your function.

var a = [1,5,6,7,3,8,12];

var b = [3,5,7];

var has_all = b.every(function(val) {
    return a.indexOf(val) !== -1;
});

alert(has_all);
six fingered man
  • 2,460
  • 12
  • 15
1

Use prototype property to reuse the function every case you need it.

Array.prototype.contains = function (n) {
  var s;
    for(s in this){
      if (this.hasOwnProperty(s) && n.indexOf(this[s]) !== -1) {
        return true;
      }
    }
  return false;
};

var a = [1, 5, 6, 7, 3, 8, 12],
  b = [1, 2, 11];
console.log(a.contains(b));
ariel_556
  • 368
  • 1
  • 9