Linked Questions

4932 votes
62 answers
3.6m views

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++...
brad's user avatar
  • 74.8k
3 votes
3 answers
4k views

javascript - check if string is in a array without case sensitive [duplicate]

I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem. JS: input = document.getElementById("input").value; ...
Roy Berris's user avatar
  • 1,562
2 votes
1 answer
204 views

check if a string is part of an array - javascript [duplicate]

I want to be able to see if "megan" is a part of the array people. Though, i can only do this by using people[2]. Here is my code: var people = new Array("jack","ian"); document.write(people[0]); ...
igetstuckalot's user avatar
838 votes
8 answers
1.2m views

Best way to find if an item is in a JavaScript array? [duplicate]

What is the best way to find if an object is in an array? This is the best way I know: function include(arr, obj) { for (var i = 0; i < arr.length; i++) { if (arr[i] == obj) return ...
zimbatm's user avatar
  • 9,388
281 votes
21 answers
354k views

JavaScript equivalent of PHP's in_array()

Is there a way in JavaScript to compare values from one array and see if it is in another array? Similar to PHP's in_array function?
roflwaffle's user avatar
  • 30.3k
7 votes
3 answers
63k views

How to check if the JSON Object array contains the value defined in an array or not?

I have the following JSON data. categories = [ {catValue:1, catName: 'Arts, crafts, and collectibles'}, {catValue:2, catName: 'Baby'}, {catValue:3, catName: 'Beauty and fragrances'}, {...
Kiran Shahi's user avatar
  • 7,874
0 votes
4 answers
37k views

How to check if the id exists in the array of ids javascript

I have an object like: item: { b: null c: "asd" i: 10 q: 10 s: Array [237,241]} Also I have an array of ids: var ids = [237, 238, 239, 240, 242, 243...] I dont know how to ...
wezeweze's user avatar
  • 131
3 votes
3 answers
21k views

How to match a variable value against an array of values in Javascript?

I have an array of values with names: names = ['Jim', 'Jack', 'Fred'] I have also received a value from a function, which is a name in string form, such as: returnedValue = 'Jim' How can I run the ...
Anthony's user avatar
  • 13.9k
-1 votes
5 answers
696 views

JavaScript - Detect string from array in input and output the string

I want to detect if a user types one of the strings of my list and then output in what string the user typed in the console. HTML <input id="input"></input> JS window.onload = function(...
Tim's user avatar
  • 165
3 votes
2 answers
116 views

Javascript - indexOf not so specific

There is a way to lower the sensibility of the indexOf? Like this var arr = ["Hello", "stackoverflow"] var idx = arr.indexOf("stack") And get idx = 1 instead of idx = -1
urco's user avatar
  • 189
3 votes
3 answers
134 views

match a string using all the strings in array

Hello, I'm trying to make a simple matching game in javascript. If the the user inserts the text president goes crazy in any way that contains every strings in word_tmp, then the word_tmp becomes ...
Adam Ramadhan's user avatar
-2 votes
2 answers
109 views

How can I check if the user input with the database information?

I have a JavaScript programme that randomly selects an image of an object from the database and display that image to the user. The image is saved with the object name (i.e. apple.gif) I am using the ...
james's user avatar
  • 1