Related documents: http://www.w3school.com.cn/jsref/jsref_splice.asp
BUG: When the shopping cart calculates the price. Add the product is no problem, the price is always calculated error when reducing the commodity.
The investigation found that the splice method was called when the total price of the product was reduced, causing the array of the loop to change and the price calculation to be incorrect.
Modify the method: When the total price calculation is complete, then manipulate the array.
On the code:
1 //Error code2$.each (Categoryjson.shop_cart_consume,function(i,proditem) {3 //s_is_new_user_parse 1 new users 2 old users4 if(S_is_new_user_parse = = ' 1 ' && parseint (proditem.unit_price_discount) > 0){5Totalprice + = number (Proditem.unit_price_discount *proditem.count);6}Else{7Totalprice + = number (Proditem.unit_price *proditem.count);8 }9 if(proditem.ware_sku_id = =product.ware_sku_id) {TenProditem.count--; One if(Proditem.count <= 0){ ACategoryJson.shop_cart_consume.splice (i,1); -}Else{ -Categoryjson.shop_cart_consume[i] =Proditem; the } - } -});
1 //Correct code2 //stores the subscript of the array minus the elements. 3 vararrayindex;4 //use a variable to identify the subscript starting at 0. 5 varBeginindex = 0;6$.each (Categoryjson.shop_cart_consume,function(i,proditem) {7 //s_is_new_user_parse 1 new users 2 old users8 if(S_is_new_user_parse = = ' 1 ' && parseint (proditem.unit_price_discount) > 0){9Totalprice + = number (Proditem.unit_price_discount *proditem.count);Ten}Else{ OneTotalprice + = number (Proditem.unit_price *proditem.count); A } - if(proditem.ware_sku_id = =product.ware_sku_id) { -Proditem.count--; the if(Proditem.count <= 0){ - //CategoryJson.shop_cart_consume.splice (i,1); -Arrayindex =i; -}Else{ +Categoryjson.shop_cart_consume[beginindex] =Proditem; -beginindex++; + } A } at }); -CategoryJson.shop_cart_consume.splice (arrayindex,1);
A pit of the JavaScript splice method.