A pit of the JavaScript splice method.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.