gpt4 book ai didi

javascript - 如何以编程方式向 JS 对象添加内容?

转载 作者:行者123 更新时间:2023-11-28 20:04:10 24 4
gpt4 key购买 nike

我正在使用 Shopify,并且必须将 Qubit 添加到客户网站。

在任何给定时间,都有一个“购物篮”对象显示用户购物车中的产品。

如何动态地将对象插入到这个 json 结构中?

  "line_items": [{
"product": {
"id": "1234567890",
"sku_code": "0987654321",
"url": "product.html",
"name": "Sparkly Shoes",
"description": "Description about this product",
"manufacturer": "The Shoe Co",
"category": "Shoe",
"subcategory": "Heels",
"color": "n/a",
"stock": 3,
"size": "6",
"currency": "GBP",
"unit_price": 130,
"unit_sale_price": 130,
"voucher": "MYVOUCHER1"
},
"quantity": 1,
"subtotal": 130,
"total_discount": 0
}, {
"product": {
"id": "1234567890",
"sku_code": "0987654321",
"url": "product-dress.html",
"name": "Red Dress",
"description": "Description about this product",
"manufacturer": "The Dress Co",
"category": "Dresses",
"subcategory": "Red dresses",
"color": "n/a",
"stock": 3,
"size": "6",
"currency": "GBP",
"unit_price": 200,
"unit_sale_price": 150,
"voucher": "MYVOUCHER1"
},
"quantity": 1,
"subtotal": 200,
"total_discount": 50
}, {
"product": {
"id": "1234567890",
"sku_code": "0987654321",
"url": "product-swimwear.html",
"name": "Bikini",
"description": "Description about this product",
"manufacturer": "The Bikini Co",
"category": "Swimwear",
"subcategory": "Bikini",
"color": "n/a",
"stock": 3,
"size": "6",
"currency": "GBP",
"unit_price": 100,
"unit_sale_price": 100,
"voucher": "MYVOUCHER1"
},
"quantity": 1,
"subtotal": 100,
"total_discount": 0
}]
},

摘自:http://tools.qubitproducts.com/uv/demosite/guide.html

它可以是一种产品,也可以是两种产品等...我如何动态迭代购物车中的可用产品(伪代码很好),然后显示一些值?

<小时/>

如果我定义上面的 json 结构,我将如何动态地执行它?只是在代码之间添加一个循环吗? (感觉很脏?)

最佳答案

一旦您获得了数据 - 它应该相当简单(基于上述网站的数据)-

window.universal_variable["basket"]["line_items"].push(myProductObj);

myProductObj 将如下所示:

{
"product": {
"id": "1234567890",
"sku_code": "0987654321",
"url": "product.html",
"name": "Sparkly Shoes",
"description": "Description about this product",
"manufacturer": "The Shoe Co",
"category": "Shoe",
"subcategory": "Heels",
"color": "n/a",
"stock": 3,
"size": "6",
"currency": "GBP",
"unit_price": 130,
"unit_sale_price": 130,
"voucher": "MYVOUCHER1"
},
"quantity": 1,
"subtotal": 130,
"total_discount": 0
}

关于javascript - 如何以编程方式向 JS 对象添加内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21127546/

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