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 に答える