67

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 = productPrice;
 }else {
  arr.push({id: productID, price: productPrice})
 }
}

I guess the code above explains what I want in really simple way. I would imagine this $.map would work like this but unfortunately I couldn't get results with this.

What is the most simple and elegant way to do this? Do I truly go through all array just to find if a key's value is there or not?

Does Jquery has something like isset($array['key'])?

EDIT

I tried to use inArray but it keeps adding object to 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)});
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Revenant
  • 2,942
  • 7
  • 30
  • 52

5 Answers5

232

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

if ($.inArray('example', myArray) != -1)
{
  // found it
}
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
jbabey
  • 45,965
  • 12
  • 71
  • 94
  • 3
    Too bad community up-voting couldn't five years later result in something being marked as the right answer. :) – David Feb 22 '16 at 23:18
  • They should be marking the correct answers themselves by looking at the up votes after x amount of time –  Jan 21 '17 at 22:31
  • 5
    Actually `$.inArray` performs a === instead of ==. So if you do `$.inArray( "4", [1,2,3,4])` you will get -1 – Yuri Mar 02 '17 at 09:46
25

If you want to do it using .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 handles each element separately. The .inArray() proposed in other answers is probably the more efficient way to do it.

Lycha
  • 9,937
  • 3
  • 38
  • 43
19

jQuery has the inArray function:

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

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
BZink
  • 7,687
  • 10
  • 37
  • 55
16
    if ($.inArray('yourElement', yourArray) > -1)
    {
        //yourElement in yourArray
        //code here

    }

Reference: Jquery Array

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

Hueston Rido
  • 809
  • 1
  • 11
  • 16
8

Try jQuery.inArray()

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

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0

Example Code :

<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
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • This answer should be the accepted one. For those in the present future, use `jQuery.inArray()` instead of `$.inArray()` – Nate Jun 13 '20 at 03:49