"in" in javascript / jQuery

Is Javascript or jQuery a bit of an in instruction in Python?

"a" to "dea" β†’ True

Googling for the word in is hopeless :(

+4
source share
5 answers

It has an in operator, but it is limited only by object keys:

 var object = { a: "foo", b: "bar" }; // print ab for (var key in object) { print(key); } 

And you can also use it for checks like this:

 if ("a" in object) { print("Object has a property named a"); } 

To check the string, although you need to use the indexOf () method:

 if ("abc".indexOf("a") > -1) { print("Exists"); } 
+29
source

you will need to use indexOf

eg

"dea".indexOf("a"); will return 2

If it is not in the element, it will return -1

I think this is what you need.

+6
source

Sounds like you need regular expressions!

 if ("dea".match(/a/)) { return true; } 
+2
source

What about indexOf ?

+1
source

Using the indexOf function indexOf you can expand the string like this:

 String.prototype.in = function (exp) { return exp.indexOf(this) >= 0; } if ("ab".in("abcde")) { //true } 
+1
source

All Articles