JQuery by checking if a value exists in an array or not

I believe this question will be quite easy for those who played with java script / jquery.

var arr = new Array(); $.map(arr, function() { if (this.id == productID) { this.price = productPrice; }else { arr.push({id: productID, price: productPrice}) } } 

I assume the code above explains what I want is really simple. I would suggest that this $ .map would work like that, but unfortunately I could not get the results with this.

What is the easiest and most elegant way to do this? Am I really looking through the entire array to see if there is a key value or not?

Is there something like isset($array['key']) in jQuery?

EDIT

I tried using inArray, but it continues to add the object to the array, even if there is a match.

 if ( $.inArray(productID, arr) > -1) { var number = $.inArray(productID, arr); orderInfo[number].price = parseFloat(productPrice); }else { orderInfo.push({id:productID, price:parseFloat(productPrice)}); } 
+45
javascript jquery arrays loops
Oct 24 '11 at 19:36
source share
5 answers

If you want to do this with .map() or just want to know how it works, you can do it like this:

 var added=false; $.map(arr, function(elementOfArray, indexInArray) { if (elementOfArray.id == productID) { elementOfArray.price = productPrice; added = true; } } if (!added) { arr.push({id: productID, price: productPrice}) } 

The function processes each element separately. .inArray() suggested in other answers is probably a more efficient way to do this.

+17
Oct 24 '11 at 19:43
source share

http://api.jquery.com/jQuery.inArray/

 if ($.inArray('example', myArray) != -1) { // found it } 
+206
Oct 24 '11 at 19:39
source share

jQuery has an inArray function:

http://api.jquery.com/jQuery.inArray/

+18
Oct 24 '11 at 19:38
source share
  if ($.inArray('yourElement', yourArray) > -1) { //yourElement in yourArray //code here } 

Link: Jquery Array

The $ .inArray () method is similar to its own .indexOf () JavaScript method, in which it returns -1 when it does not find a match. If the first element inside the array matches the value, $ .inArray () returns 0.

+15
Nov 18 '14 at 1:39
source share

Try jQuery.inArray()

Here is the jsfiddle link using the same code: http://jsfiddle.net/yrshaikh/SUKn2/

The $ .inArray () method is similar to its own .indexOf () JavaScript method, in which it returns -1 when it does not find a match. If the first element in the array matches the value, $ .inArray () returns 0

Code example :

 <html> <head> <style> div { color:blue; } span { color:red; } </style> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <div>"John" found at <span></span></div> <div>4 found at <span></span></div> <div>"Karl" not found, so <span></span></div> <div> "Pete" is in the array, but not at or after index 2, so <span></span> </div> <script> var arr = [ 4, "Pete", 8, "John" ]; var $spans = $("span"); $spans.eq(0).text(jQuery.inArray("John", arr)); $spans.eq(1).text(jQuery.inArray(4, arr)); $spans.eq(2).text(jQuery.inArray("Karl", arr)); $spans.eq(3).text(jQuery.inArray("Pete", arr, 2)); </script> </body> </html> 

Output:

 "John" found at 3
 4 found at 0
 "Karl" not found, so -1
 "Pete" is in the array, but not at or after index 2, so -1
+7
Feb 04 '13 at 8:09
source share



All Articles