如何将金额乘积与初始乘积数组本地存储相加?
我正在学习 javascript 课程,但我被困在某个地方了!
我创建了一个定制沙发店,目前有 3 个页面:包含所有产品的欢迎页面、每个产品的定制页面和购物车页面。
我将购物车产品存放在本地存储中。
但我遇到了一个问题。
在产品页面中,我们已经可以选择要添加到购物车页面中的同一商品的数量。如果购物车页面和本地存储中的产品数组为空,则当我们添加一定数量的产品时,它只会获取输入的值,然后发送到本地存储。
但是,例如,如果我的购物车中已经有“蓝色沙发”x30,并且我返回到产品页面,并且我在本地存储中发送 40x“蓝色沙发”,而不是对“蓝色沙发”的现有数量进行求和” +我刚刚发送的内容,它只是更新了数量,所以删除最初的 30x“蓝色沙发”并用 40x“蓝色沙发”替换它。
我该如何修复它?提前谢谢!
if (productArray == null) {
productArray = [];
productArray.push(fusionproductAndColor);
productArray.amount += amountInput.value;
localStorage.setItem("product", JSON.stringify(productArray));
} else if (productArray != null) {
for (i = 0; i < productArray.length; i++){
console.log("test");
if (productArray[i]._id == productDetails._id && productArray[i].color == select.value) {
return (
productArray[i].amount = amountInput.value,
productArray.amount = this.amount += amountInput.value,
console.log("quantite"),
localStorage.setItem("product",JSON.stringify(productArray))
(productArray = JSON.parse(localStorage.getItem("product")))
);
}
}
for (i = 0; i < productArray.length; i++) {
if (productArray[i]._id == productDetails._id && productArray[i].color != productDetails.color){
return (
productArray.push(fusionproductAndColor),
localStorage.setItem("product",JSON.stringify(productArray))
(productArray = JSON.parse(localStorage.getItem("product")))
);
}
}
for (i = 0; i < productArray.length; i++) {
if (productArray[i]._id != productDetails._id){
return (
productArray.push(fusionproductAndColor),
localStorage.setItem("product",JSON.stringify(productArray))
(productArray = JSON.parse(localStorage.getItem("product")))
);
i'm following a course to learn javascript, but i got stuck somewhere !
I've created a custom sofa shop, with 3 pages for the moment : welcome page with all products, a custom page for each product, and a cart page.
I stock the cart products in the localstorage.
But I got a problem.
In the product page, we already have the option to choose what amount of the same article we want to add into the cart page. If the cart page, and the productarray in localstorage are empty, when we are adding an certain amount of a product, it will simply take the value of the input, and send to the local storage.
But, if i already have, for example, "blue sofa" x30 in my cart, and i return to the product page, and i send 40x "blue sofa" in the localstorage, instead of sum the already existing amount of "blue sofa" + what i just sent, it just update the amount, so delete the initial 30x "blue sofa" and replace it with 40x "blue sofa".
How can i fix it ? Thx in advance !
if (productArray == null) {
productArray = [];
productArray.push(fusionproductAndColor);
productArray.amount += amountInput.value;
localStorage.setItem("product", JSON.stringify(productArray));
} else if (productArray != null) {
for (i = 0; i < productArray.length; i++){
console.log("test");
if (productArray[i]._id == productDetails._id && productArray[i].color == select.value) {
return (
productArray[i].amount = amountInput.value,
productArray.amount = this.amount += amountInput.value,
console.log("quantite"),
localStorage.setItem("product",JSON.stringify(productArray))
(productArray = JSON.parse(localStorage.getItem("product")))
);
}
}
for (i = 0; i < productArray.length; i++) {
if (productArray[i]._id == productDetails._id && productArray[i].color != productDetails.color){
return (
productArray.push(fusionproductAndColor),
localStorage.setItem("product",JSON.stringify(productArray))
(productArray = JSON.parse(localStorage.getItem("product")))
);
}
}
for (i = 0; i < productArray.length; i++) {
if (productArray[i]._id != productDetails._id){
return (
productArray.push(fusionproductAndColor),
localStorage.setItem("product",JSON.stringify(productArray))
(productArray = JSON.parse(localStorage.getItem("product")))
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个
if
块中,尝试:productArray[0].amount += amountInput.value;
而不是:
productArray.amount += amountInput.value;< /code>
(数组没有名为 amount 的属性,但刚刚存储在新数组的元素 0 中的对象可能有)。
我本以为这会抛出一个错误,在浏览器的开发人员控制台上可见。
In your first
if
block, try:productArray[0].amount += amountInput.value;
instead of:
productArray.amount += amountInput.value;
(arrays do not have a property called amount but an object you just stored in element 0 of the new array could have).
I would have expected this to throw an error, visible on the developer console in your browser.