0

我有几个对象作为这种结构: 在此处输入图像描述

然后我创建一个函数来更新与搜索 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);结果未定义。

4

4 回答 4

3

我认为您不需要 for in 循环。看起来你的参数 json 已经是一个对象,所以不需要循环,只要去 json.ID

于 2013-09-04T05:01:18.610 回答
0

您可以直接访问属性,如下所示:

if (json.ID == itemID) {
    json.QuantityInCart = val;
}
于 2013-09-04T05:07:19.010 回答
0

在我做了一个小提琴测试之后,之前的链接似乎并不适用。我还更新了第一个示例。

http://jsfiddle.net/AFZhT/

这也假设你已经做了类似的事情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;
}
于 2013-09-04T05:07:28.880 回答
0

您拥有的是一个 javascript 对象,而不是一个数组,并且您正在处理属性而不是索引。

我认为您要完成的是在不知道名称的情况下访问对象的属性。为此,您可以使用 for ... in 循环:

for(key in data) {
    if(data.hasOwnProperty(key)) {
        var value = data[key];
        //do something with value;
    }
}

如果你想使用简单的 javascript 那么那个时候你可以使用上面的代码。

于 2013-09-04T06:08:59.087 回答