Javascript Jquery,检查数组中是否存在值

Javascript Jquery,检查数组中是否存在值,javascript,jquery,arrays,loops,Javascript,Jquery,Arrays,Loops,我相信对于那些使用java脚本/jquery的人来说,这个问题将相当容易 var arr = new Array(); $.map(arr, function() { if (this.id == productID) { this.price = productPrice; }else { arr.push({id: productID, price: productPrice}) } } 我想上面的代码用非常简单的方式解释了我想要的东西。我可以想象这个$.map会像这样工

我相信对于那些使用java脚本/jquery的人来说,这个问题将相当容易

var arr = new Array();

$.map(arr, function() {
 if (this.id == productID) {
   this.price = productPrice;
 }else {
  arr.push({id: productID, price: productPrice})
 }
}
我想上面的代码用非常简单的方式解释了我想要的东西。我可以想象这个$.map会像这样工作,但不幸的是,我不能用这个得到结果

最简单、最优雅的方法是什么?我是否真的遍历了所有数组,只是为了找到一个键的值是否存在

Jquery是否有类似于isset($array['key'])的功能

编辑


我尝试使用inArray,但它会不断向数组中添加对象,即使存在匹配项

if ( $.inArray(productID, arr) > -1) {
   var number = $.inArray(productID, arr);
   orderInfo[number].price = parseFloat(productPrice);
}else {
   orderInfo.push({id:productID, price:parseFloat(productPrice)});
}

jQuery具有
inaray
功能:


如果您想使用
.map()
或只是想知道它是如何工作的,您可以这样做:

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})
}
该函数分别处理每个元素。其他答案中提出的
.inArray()
可能是更有效的方法。

试试看

下面是一个使用相同代码的示例:

$.inArray()方法类似于JavaScript的本机.indexOf()方法,因为它在未找到匹配项时返回-1。如果数组中的第一个元素与值匹配,$.inArray()返回0

示例代码

<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>

div{颜色:蓝色;}
span{颜色:红色;}
“约翰”在
4于
“卡尔”没有找到,所以
“Pete”在数组中,但不在索引2处或之后,所以
var arr=[4,“皮特”,8,“约翰”];
var$spans=$(“span”);
$span.eq(0).text(jQuery.inArray(“John”,arr));
$span.eq(1.text)(jQuery.inArray(4,arr));
$span.eq(2).text(jQuery.inArray(“Karl”,arr));
$span.eq(3).text(jQuery.inArray(“Pete”,arr,2));
输出:

"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 “约翰”在3点被发现 在0处找到4 未找到“Karl”,因此-1 “Pete”在数组中,但不在索引2处或之后,所以为-1 参考:


$.inArray()方法类似于JavaScript的本机.indexOf()方法,因为它在未找到匹配项时返回-1。如果数组中的第一个元素与值匹配,$.inArray()返回0。

我尝试使用inArray,但似乎在某个点上做错了。它一直在添加数据。@Revenant就是一个例子。只需额外添加1个信息
added=true
必须在条件内。这是我正在寻找的,非常感谢coool解决方案太糟糕了社区投票不能在五年后将某些内容标记为正确答案。:)他们应该自己通过查看x时间量后的向上投票来标记正确答案。实际上,
$。inArray执行a===而不是==。因此,如果您执行
$。inArray(“4”,[1,2,3,4])
您将得到-1
$。inArray()
在大多数情况下不起作用。最好使用
jQuery.inArray()
这个答案应该是可以接受的。对于现在和将来的用户,请使用
jQuery.inArray()
而不是
$.inArray()
"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
    if ($.inArray('yourElement', yourArray) > -1)
    {
        //yourElement in yourArray
        //code here

    }