Friday, April 6, 2018

JavaScript & Python: Check if Item is in Array (or List)

JavaScript

We can use indexOf method to do that.

Using indexOf, we'll expect integer output. Returned value -1 (negative one) is to tell that the item isn't present in the array.

In latest version of ECMA (latest browsers), we can use includes method.

Using includes, we'll expect boolean output (true / false).

For jQuery, we can use $.inArray().


Python

Well, the phrase itself, "check if item in list", then we can use in operator to do that.

Using that method, we'll expect boolean output (True / False).

No comments:

Post a Comment

Tell me what you think...