我有几个对象作为这个结构:
然后我创建一个函数来更新与搜索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);
结果未定义。
答案 0 :(得分:3)
我认为你不需要for循环。看起来你的参数json已经是一个对象,所以不需要循环,只需去json.ID
答案 1 :(得分:0)
您可以直接访问属性,如下所示:
if (json.ID == itemID) {
json.QuantityInCart = val;
}
答案 2 :(得分:0)
之前的链接似乎并不适用于我做了一个小提琴来测试它。我还更新了第一个例子。
这也假设您已完成类似var json = JSON.parse(data)
如果返回一个对象数组,则您的数据应类似于以下内容。这将作为json参数传递到setQuantityInCartByID函数:
var 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)/"}];
在你的页面中,以下内容将从上面接收json对象,现在它应该有一个长度。
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;
}
以下是您似乎一直在考虑的旧方式:
function setQuantityInCartByID(json, itemID, val){
for(var i = 0; i <json.length;i++)
{
console.log(json[i].ID);
if(json[i].ID === itemID){
json[i].QuantityInCart = val;
break;
}
}
return json;
}
答案 3 :(得分:0)
你所拥有的是一个javascript对象,而不是一个数组,并且你正在处理属性而不是指标。
我认为您要完成的是在不知道名称的情况下访问对象的属性。要做到这一点,你可以使用for ... in循环:
for(key in data) {
if(data.hasOwnProperty(key)) {
var value = data[key];
//do something with value;
}
}
如果你想使用简单的javascript,那么你可以使用上面的代码。