1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <title>删去数组中指定元素,组成新数组</title> 8 </head> 9 <body> 10 11 </body> 12 <script> 13 var arr = [1,2,3,4,2,5,6,2,7,2];//设定一个数组 14 var brr = [];//新生成的数组 15 var a = b = 0; 16 for (const i in arr) {//遍历 17 if (arr[i]==2) {//判断数组arr中下标为i的元素是否为2 18 delete arr[i]; //如果是删除 19 }else{ 20 a = arr[i]; 21 brr[b] = a;//如果不是插入新数组 22 b++; 23 } 24 } 25 for (const i in brr) { 26 document.write(brr[i]);//遍历输出 27 } 28 </script> 29 </html>