我有几个对象作为这种结构:
然后我创建一个函数来更新与搜索 ID 匹配的项目的数量:
function setQuantityInCartByID(json, itemID, val){
for(var i in json){
console.log(json[i].ID);
if(json[i].ID == itemID){
json[i].QuantityInCart = val;
break;
}
}
return json;
}
这是我的 JSON;
{"DepartmentID":12,"CategoryID":117,"BrandID":19,"BrandImage":" ","BrandName":"General","ID":708,"Name":"Grand 6Port Power Center","PictureName":"http://111.92.241.110/wwwProducts/unknown.PNG","Notes":"","PriceShow":"$10.00","Price":0,"PriceA":0,"PriceB":0,"PriceC":0,"WebsitePrice":0,"Quantity":2,"QuantityInCart":2,"LastUpdated":"/Date(1378108144050)/","Active":1,"PriceLevel":0,"NewProductImage":"http://111.92.241.110/wwwProducts/newproduct.png","isNewProduct":false,"isInStock":"10 Available in Stock(s)!","NewArrival":0,"ExpireNewArrival":"/Date(-62135596800000)/","NewPromotion":0,"ExpireNewPromotion":"/Date(-62135596800000)/"}
{"DepartmentID":12,"CategoryID":117,"BrandID":19,"BrandImage":" ","BrandName":"General","ID":709,"Name":"Grand 6Port Power Center","PictureName":"http://111.92.241.110/wwwProducts/unknown.PNG","Notes":"","PriceShow":"$10.00","Price":0,"PriceA":0,"PriceB":0,"PriceC":0,"WebsitePrice":0,"Quantity":2,"QuantityInCart":2,"LastUpdated":"/Date(1378108144050)/","Active":1,"PriceLevel":0,"NewProductImage":"http://111.92.241.110/wwwProducts/newproduct.png","isNewProduct":false,"isInStock":"10 Available in Stock(s)!","NewArrival":0,"ExpireNewArrival":"/Date(-62135596800000)/","NewPromotion":0,"ExpireNewPromotion":"/Date(-62135596800000)/"}
{"DepartmentID":12,"CategoryID":117,"BrandID":19,"BrandImage":" ","BrandName":"General","ID":710,"Name":"Grand 6Port Power Center","PictureName":"http://111.92.241.110/wwwProducts/unknown.PNG","Notes":"","PriceShow":"$10.00","Price":0,"PriceA":0,"PriceB":0,"PriceC":0,"WebsitePrice":0,"Quantity":2,"QuantityInCart":2,"LastUpdated":"/Date(1378108144050)/","Active":1,"PriceLevel":0,"NewProductImage":"http://111.92.241.110/wwwProducts/newproduct.png","isNewProduct":false,"isInStock":"10 Available in Stock(s)!","NewArrival":0,"ExpireNewArrival":"/Date(-62135596800000)/","NewPromotion":0,"ExpireNewPromotion":"/Date(-62135596800000)/"}
问题:console.log(json[i].ID);
结果未定义。