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