Jquery, checking if a value exists in array or not
This question already has an answer here:
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.
http://api.jquery.com/jQuery.inArray/
if ($.inArray('example', myArray) != -1)
{
// found it
}
jQuery has the inArray
function:
http://api.jquery.com/jQuery.inArray/
链接地址: http://www.djcxy.com/p/13034.html上一篇: 测试Javascript数组中的值
下一篇: jquery,检查数组中是否存在一个值