0

I want to check if an array contains an array of values rather then a value, for example

var arr = [1, 2, 3];
arr.contains([1, 2]); // returns true
// and
arr.contains([1, 5]); // must be false

There is a method called contains in underscore _.contains but it works like .indexof() in javascript instead it returns true and false and only find a value and not an array

Also there is a method Array.prototype.includes() in javascript that also take single input rather of an array

3
  • "I want to check if an array contains an array of values rather then a value" Is requirement to check if array contains any value that is an array ; or if array contains values which are all arrays ? Commented Mar 8, 2016 at 7:23
  • @guest271314 sir actually I want to check if second array has all the keys present in first array I want to check with AND Commented Mar 9, 2016 at 7:34
  • for example [1,2,3,4].contains([1,4]) must return true where as [1,2,3,4].contains([1,9]) must return false, hope this makes sense, I'm for now using underscores intersection and second array with intersection result it must be same, its still long procedure.... Commented Mar 9, 2016 at 7:39

5 Answers 5

1

actually I want to check if second array has all the keys present in first array I want to check with AND

for example [1,2,3,4].contains([1,4]) must return true where as [1,2,3,4].contains([1,9]) must return false, hope this makes sense, I'm for now using underscores intersection and second array with intersection result it must be same, its still long procedure....

You could use Array.prototype.includes() within a for loop

var arr = [1,2,3,4];

function contains(arr) {
  for (var i = 0, included = true; i < arr.length
       ; included = included && this.includes(arr[i]), i++);
  return included
}

Array.prototype.contains = contains;

console.log(arr.contains([1, 4]), arr.contains([9, 1]), arr.contains([1, 5]))

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a combination of every and indexOf

[1, 2].every(function(e) { return [1, 2, 3].indexOf(e) !== -1 });

would evaluate to true and

[1, 5].every(function(e) { return [1, 2, 3].indexOf(e) !== -1 });

would evaluate to false

The syntax would be a lot shorter in ES6 with arrow functions

[1, 2].every(e => [1, 2, 3].indexOf(e) !== -1)

Comments

0

Try this simple example

var arr = [1, 2, 3];
var searchArr = [1,2];
var isSearchArrAvailable = searchArr.filter( function(value){ if ( arr.indexOf( value ) == -1 ) { return true } else { return false; } } ).length == 0;
console.log( "is SearchArr Available " + isSearchArrAvailable );//this will print true

while var searchArr = [1,5] will print false.

You can refactor it into a function

function hasEveryElement(arr, searchArr)
{
   return searchArr.filter( function(value){ if ( arr.indexOf( value ) == -1 ) { return true } else { return false; } } ).length == 0;
}
console.log( hasEveryElement([1,2,3], [1,2]) ) ;
console.log( hasEveryElement([1,2,3], [1,4]) ) ;

Comments

0

You can use every:

function contains(arr, arr2) {
  return arr2.every(function (el) {
    return arr.indexOf(el) > -1;
  });
}

var arr = [1, 2, 3];
contains(arr, [1, 2]); // true
contains(arr, [1, 5]); // false

Alternatively, you could add a method to the prototype if you wanted the [].includes-style syntax, but call it something else - I've used hasAll.

if (!('hasAll' in Array.prototype)) {
  Array.prototype.hasAll = function(arr) {
    return arr.every(function(el) {
      return this.indexOf(el) > -1;
    }, this);
  }

arr.hasAll([1, 2])); // true
arr.hasAll([1, 5])); // false

DEMO

Comments

0
Try this:
var arr = [1,2,3];
var temparr=[1,2];
var st=ArrOfArrSt(arr,temparr); 

function ArrOfArrSt(arr,temparr){
    var stArr= new Array(); 
    if(temparr.length>0)
        for(j in temparr){
                if(arr.indexOf(temparr[j])==-1){
                   stArr.push('0'); 
                }
        }
  return (stArr.length>0)?0:1; //whole array 0=not belong to / 1=belong to  
}

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.