javascript - increase number by 1 if order_id found (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));
how can alter quantity of order if order_id exist in localstorage? above code snippet insert set of array object although quantity part correct.
since, want update array value when id
matches, don't create new object in case.
instead change if part of code -
$.each(arr, function(){ if(obj.order_id == this.order.id){ this.qty = parseint(this.qty) + 1; //assuming want edit qnty value }
also ensure push obj
in else block only.
Comments
Post a Comment