การใช้ลูปและการแยกเพื่อแทนที่และลบองค์ประกอบได้รับการทดสอบง่ายมาก แต่ใช้งานได้จริงโดยเฉพาะอย่างยิ่งเหมาะสำหรับผู้เริ่มต้น
<! doctype> <html> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> ใช้ลูปและแยกเพื่อแทนที่และลบองค์ประกอบ </title> Array ("AB", "AA", "BC", "AB", "SS", "AB", "SS", "AB", "SS", "DD", "AB", "AA", "AA", "AA", "AA"); ในขณะที่ (arr.indexof ("ab")! = -1) {console.log ("1"+arr); arr.splice (arr.indexof ("ab"), 1, "**"); } console.log (arr); ในขณะที่ (arr.indexof ("**")! = -1) {console.log (arr); arr.splice (arr.indexof ("**"), 1); } console.log (arr); </script> </head> <body> </body> </html>IE8 ไม่สนับสนุนดัชนีและประกบกัน