Javascript Jquery,检查数组中是否存在值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7880972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery, checking if a value exists in array or not
提问by Revenant
I believe this question will be fairly easy for the ones who played around with java script / jquery.
我相信这个问题对于那些玩过 java 脚本/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.
我想上面的代码以非常简单的方式解释了我想要的东西。我想这个 $.map 会像这样工作,但不幸的是我无法得到结果。
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'])
?
Jquery 有类似的东西isset($array['key'])
吗?
EDIT
编辑
I tried to use inArray but it keeps adding object to array even if there is a match.
我尝试使用 inArray 但即使存在匹配项,它也会不断向数组添加对象。
if ( $.inArray(productID, arr) > -1) {
var number = $.inArray(productID, arr);
orderInfo[number].price = parseFloat(productPrice);
}else {
orderInfo.push({id:productID, price:parseFloat(productPrice)});
}
采纳答案by Lycha
If you want to do it using .map()
or just want to know how it works you can do it like this:
如果你想使用它.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})
}
The function handles each element separately. The .inArray()
proposed in other answers is probably the more efficient way to do it.
该函数分别处理每个元素。将.inArray()
在其他的答案提出的可能是做的更有效的方法。
回答by jbabey
http://api.jquery.com/jQuery.inArray/
http://api.jquery.com/jQuery.inArray/
if ($.inArray('example', myArray) != -1)
{
// found it
}
回答by BZink
回答by Hueston Rido
if ($.inArray('yourElement', yourArray) > -1)
{
//yourElement in yourArray
//code here
}
Reference: Jquery Array
参考:Jquery 数组
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.
$.inArray() 方法类似于 JavaScript 的原生 .indexOf() 方法,因为它在找不到匹配项时返回 -1。如果数组中的第一个元素与 value 匹配,则 $.inArray() 返回 0。
回答by Yasser Shaikh
Try jQuery.inArray()
Here is a jsfiddle linkusing the same code : http://jsfiddle.net/yrshaikh/SUKn2/
这是使用相同代码的jsfiddle 链接: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
$.inArray() 方法类似于 JavaScript 的原生 .indexOf() 方法,因为它在找不到匹配项时返回 -1。如果数组中的第一个元素与 value 匹配,则 $.inArray() 返回 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