deleting a specific object in a javascript array -
im doing a test on removing objects inside an array ... because it is a test, it is an informal Code is.
& lt; Script type = "text / javascript" & gt; // Start the array and objects var free = new array (); Var z = {test1: "test0", test2: "test2"} fruit.push (z); Var z2 = {test1: "test1", test2: "test2"} fruit.push (z2); Var z3 = {test1: "test2", test2: "test2"} fruit. Push (z3); Var z4 = {test1: "test3", test2: "test2"} fruit.push (z4); Var z5 = {test1: "test4", test2: "test2"} fruit.push (z5); // display array length document. Written ("array length" + fruit length = "& lt; br & gt;"); // Troops array for frame type (var x = 0; x & lt; fruit.length; x ++) to write the Xer object object in array (frame [x] .test1 + ""); // Delete the object in the array where the variable is equal to test1 "test2" (fruit [x] .test1 == "test2") {fruit.splice (x, 1); //document.write (free length "+ fruits.lumber ++" & lt; br & gt; ");}} & lt; / script> Now this code Works fine (removing the object on the array), but it removes one after destroying one (in the above code, I want to remove the object in index 2, but it removes the object in the index
TIA :)
To change an array while you run it Oshis should not. Instead, to save the pointer to the element that you want to delete a variable, and remove it after the loop.
Comments
Post a Comment