如果发现ORDER_ID增加1号(localStorage的) [英] increase number by 1 if order_id found (localStorage)

查看:104
本文介绍了如果发现ORDER_ID增加1号(localStorage的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var arr = JSON.parse(localStorage.getItem('cart')) || [];
var obj = {};


$.each(arr, function(){
if(obj.order_id == this.order.id){
  obj.order_id = '1';
  obj.order_name = 'cake';
  obj.price = '$1.10';
  obj.qty = 1;
  obj.qty = parseInt(this.qty) + 1;
}else{
  obj.order_id = '1';
  obj.order_name = 'cake';
  obj.price = '$1.10';
  obj.qty = 1;
}
});

arr.push(obj);
localStorage.setItme('cart',JSON.stringify(arr));

我怎样才能改变一个订单的数量,如果ORDER_ID在我的localStorage已经存在?量虽然部分是正确的上述code段将插入另一套数组对象。

How can I alter the quantity of an order if the order_id already exist in my localStorage? The above code snippet will insert another set of array object although the quantity part is correct.

推荐答案

一直以来,要更新的数组值时, ID 匹配,不创建新对象这种情况下。

Since, you want to update the array value when id matches, don't create new object in this case.

而不是改变,如果你的code的一部分 -

Instead change if part of your code to -

$.each(arr, function(){
if(obj.order_id == this.order.id){
  this.qty = parseInt(this.qty) + 1; //Assuming you only want to edit qnty value
}

此外,还要确保你只else块推 OBJ

这篇关于如果发现ORDER_ID增加1号(localStorage的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆