This question already has an answer here:
这个问题已经有了答案:
- How do I check if an array includes an object in JavaScript? 40 answers
- 如何检查一个数组是否包含JavaScript中的对象?40的答案
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.
我想上面的代码解释了我想要的非常简单的方法。我可以想象这一美元。地图就像这样,但不幸的是我无法得到结果。
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)});
}
5 个解决方案
#1
17
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()可能是更有效的方法。
#2
209
http://api.jquery.com/jQuery.inArray/
http://api.jquery.com/jQuery.inArray/
if ($.inArray('example', myArray) != -1)
{
// found it
}
#3
18
jQuery has the inArray
function:
jQuery有inArray函数:
http://api.jquery.com/jQuery.inArray/
http://api.jquery.com/jQuery.inArray/
#4
15
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的native . indexof()方法,当它没有找到匹配时,它返回-1。如果数组中的第一个元素匹配值,$. inarray()返回0。
#5
7
Try jQuery.inArray()
尝试jQuery.inArray()
Here is a jsfiddle link using 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的native . indexof()方法,当它没有找到匹配时,它返回-1。如果数组中的第一个元素匹配值,$.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
#1
17
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()可能是更有效的方法。
#2
209
http://api.jquery.com/jQuery.inArray/
http://api.jquery.com/jQuery.inArray/
if ($.inArray('example', myArray) != -1)
{
// found it
}
#3
18
jQuery has the inArray
function:
jQuery有inArray函数:
http://api.jquery.com/jQuery.inArray/
http://api.jquery.com/jQuery.inArray/
#4
15
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的native . indexof()方法,当它没有找到匹配时,它返回-1。如果数组中的第一个元素匹配值,$. inarray()返回0。
#5
7
Try jQuery.inArray()
尝试jQuery.inArray()
Here is a jsfiddle link using 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的native . indexof()方法,当它没有找到匹配时,它返回-1。如果数组中的第一个元素匹配值,$.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