gpt4 book ai didi

javascript - 从javascript中的对象获取值

转载 作者:行者123 更新时间:2023-11-28 20:19:32 25 4
gpt4 key购买 nike

我有几个对象作为这种结构: enter image description here

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

最佳答案

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

关于javascript - 从javascript中的对象获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18605697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com