I believe this question will be quite easy for those who played 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 assume the code above explains what I want is really simple. I would suggest that this $ .map would work like that, but unfortunately I could not get the results with this.
What is the easiest and most elegant way to do this? Am I really looking through the entire array to see if there is a key value or not?
Is there something like isset($array['key']) in jQuery?
EDIT
I tried using inArray, but it continues to add the object to the 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)}); }
javascript jquery arrays loops
Revenant Oct 24 '11 at 19:36 2011-10-24 19:36
source share