Problems with Data deletion splice () for JavaScript arrays

Source: Internet
Author: User

Recently in doing a project encountered a JavaScript splice () Delete the problem, this problem, looked for a long time, only to find the reason. At the same time, the use of Web resources, the final solution. In my project, I want to implement this function, an element of an array is deleted.

Example: Array value myarrays This is stored in a cookie (The_cookieselectfoodorderck)

Call deletecookiedatafun(foodate,Foodetype) based on the query criteria and delete it, then convert the data to a JSON string without deleting it. stored in a cookie.

 1     //Delete Dishes at the time of order 2function Deletecookiedatafun (foodate, foodetype) {3         //read the value of the cookie first 4         varCookieck = $.cookie ('The_cookieselectfoodorderck');//Read Cookies 5         varMyarrays = eval (cookieck);//The converter converts an array string into a group object 6          7         if(Myarrays = =NULL) {//detect if the cookie has no value 8             //set to a spatial array by default 9Myarrays =NewArray ();Ten$.cookie ('The_cookieselectfoodorderck','[]', {path:"/", Expires:-1});//Delete Cookies One}Else { A              for(vari =0; i < myarrays.length; i++) { -                 //according to the order type and date -                 if(myarrays[i].foodate.tostring () = = Foodate.tostring () && myarrays[i].foodtype.tostring () = =foodetype.tostring ()) { -Myarrays.splice (i);//Remove from array -                 } +             } -             varobjstring = Json.stringify (myarrays);//Convert JSON data into strings +$.cookie ('The_cookieselectfoodorderck','[]', {path:"/", Expires:-1});//Storing Cookies A$.cookie ('The_cookieselectfoodorderck', objstring, {path:"/", Expires:1});//Add a cookie at  -         } -}

The result objstring value here is the "[]" array. Did not achieve the expected results. Then I'll find some information on the Internet. Modify for Loop Myarrays.splice (i)

1     //Delete Dishes at the time of order2 function Deletecookiedatafun (foodate, foodetype) {3         //read the value of the cookie first4         varCookieck = $.cookie ('The_cookieselectfoodorderck');//Read Cookies5         varMyarrays = eval (cookieck);//The converter converts an array string into a group object6         7         if(Myarrays = =NULL) {//detect if the cookie has no value8             //set to a spatial array by default9Myarrays =NewArray ();Ten$.cookie ('The_cookieselectfoodorderck','[]', {path:"/", Expires:-1});//Delete Cookies One}Else { A              for(vari =0; i < myarrays.length; i++) { -                 //according to the order type and date -                 if(myarrays[i].foodate.tostring () = = Foodate.tostring () && myarrays[i].foodtype.tostring () = =foodetype.tostring ()) { the  -Myarrays.splice (I,1);//Remove from array -                     i = i- 1; //you must subtract 1 from I, otherwise an element will be skipped over -                 } +             } -             varobjstring = Json.stringify (myarrays);//Convert JSON data into strings +$.cookie ('The_cookieselectfoodorderck','[]', {path:"/", Expires:-1});//Storing Cookies A$.cookie ('The_cookieselectfoodorderck', objstring, {path:"/", Expires:1});//Add a cookie at  -         } -}

If there is no i=i-1, then the array element will be deleted, it will become an empty array.

That's it, ok!.

Problems with Data deletion splice () for JavaScript arrays

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.