Linked Questions

1406 votes
18 answers
1.8m views

I need to determine if a value exists in an array. I am using the following function: Array.prototype.contains = function(obj) { var i = this.length; while (i--) { if (this[i] == obj)...
Prasad's user avatar
  • 59.7k
811 votes
9 answers
1.6m views

The function I am using now to check this is the following: function inArray(needle,haystack) { var count=haystack.length; for(var i=0;i<count;i++) { if(haystack[i]===needle){...
Francisc's user avatar
  • 81k
840 votes
8 answers
1.2m views

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,408
269 votes
18 answers
606k views

I need to determine if an object already exists in an array in javascript. eg (dummycode): var carBrands = []; var car1 = {name:'ford'}; var car2 = {name:'lexus'}; var car3 = {name:'maserati'}; var ...
Caspar Kleijne's user avatar
214 votes
16 answers
347k views

Let's say I have this: var blockedTile = new Array("118", "67", "190", "43", "135", "520"); There's more array elements but those are just few for readability purposes. Anyways, I could do a "for" ...
test's user avatar
  • 18.3k
299 votes
5 answers
643k views

I have a string array and one string. I'd like to test this string against the array values and apply a condition the result - if the array contains the string do "A", else do "B". How can I do that?
gtludwig's user avatar
  • 5,671
69 votes
5 answers
265k views

I believe this question will be fairly easy for the ones who played around with java script / jquery. var arr = new Array(); $.map(arr, function() { if (this.id == productID) { this.price = ...
Revenant's user avatar
  • 3,014
67 votes
9 answers
54k views

In SQL Server, I could say: WHERE X IN(1,2) How would you rewrite the following in JavaScript: if (X==1 || X==2) {}
Phillip Senn's user avatar
  • 47.8k
65 votes
8 answers
59k views

Is there an easier way to determine if a variable is equal to a range of values, such as: if x === 5 || 6 rather than something obtuse like: if x === 5 || x === 6 ?
Adam Templeton's user avatar
72 votes
3 answers
347k views

Currently, I'm using the forEach()-method of angular to check the new value with the array of objects. But that's the wrong approach because, for example, in the list are 20 objects. When I'm creating ...
yuro's user avatar
  • 2,229
40 votes
3 answers
102k views

I have a simple array with bank holidays: var bank_holidays = ['06/04/2012','09/04/2012','07/05/2012','04/06/2012','05/06/2012','27/08/2012','25/12/2012','26/12/2012','01/01/2013','29/03/2013','01/04/...
Nick's user avatar
  • 3,985
23 votes
7 answers
62k views

I have a variable: var code = "de"; And I have an array: var countryList = ["de","fr","it","es"]; Could someone help me as I need to check to see if the variable is inside the countryList array - my ...
ngplayground's user avatar
  • 21.8k
39 votes
3 answers
58k views

Possible Duplicates: Test for value in Javascript Array Best way to find an item in a JavaScript Array ? Javascript - array.contains(obj) I usually program in python but have recently started ...
Codahk's user avatar
  • 1,220
15 votes
2 answers
67k views

Is there a better way to check multiple items match a variable in if statement I have 3 if statements and i need to see if a item matches an array/variable named code. There are a lot of items to ...
Aaron's user avatar
  • 545
22 votes
9 answers
17k views

Possible Duplicate: array.contains(obj) in JavaScript Something like: if (mystring == "a" || mystring == "b" || mystring =="c") I was hopping to do: if (mystring in ("a", "b", "c")) is it ...
Diego's user avatar
  • 36.6k

15 30 50 per page
1
2 3 4 5
25