2

example:

var arr = ["a", "b", "c", "d", "e", "f"];

how to check if "a", "b" and "c" are in array?

i tried indexOf() but i cant check if more than 1 strings are in array...

1
  • Did you consider using a "loop"? Commented Dec 14, 2014 at 7:49

2 Answers 2

1

You are use Array.protoype.every and Array.prototype.indexOf, like this

["a", "b", "c"].every(function(currentItem) {
    return arr.indexOf(currentItem) !== -1;
});

This will return true, only if all the elements in ["a", "b", "c"] are present in arr.

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

6 Comments

will it be true if there are other strings too in that array?
@IrakliBerberashvili What do you mean by that? Can you give an example?
In which browsers is this compatible?
@Seabiscuit Do you want me to include the list of browsers which do not support these functions?
At least the earliest iteration of IE browsers that support them. That's usually the bottleneck
|
0

try like this:

var arr = ["a", "b", "c", "d", "e", "f"];
var arr1=['a','b','c'];
     for(i=0;i<arr1.length;i++){
         var a1 = arr.indexOf(arr1[i]);
            console.log(a1);
     }

or

var a = arr.indexOf("a");
console.log(a);//0
var b = arr.indexOf("b");
console.log(b);//1
var c = arr.indexOf("c");
console.log(c);//2

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.