JavaScript使用循环和分割来替换和删除元素实例

时间:2022-06-08 13:56:07

使用循环和分割来替换和删除元素,经测试很简单,但很实用,特别适合新手朋友

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>使用循环和分割来替换和删除元素</title>
<script>
var arr = new Array("ab","aa","bc","ab","ss","ab","ss","dd","ab","aa","aa","aa");
while(arr.indexOf("ab") != -1){
console.log("1"+arr);
arr.splice(arr.indexOf("ab"),1,"**");
}
console.log(arr);
while(arr.indexOf("**") != -1){
console.log(arr);
arr.splice(arr.indexOf("**"),1);
}
console.log(arr);
</script>
</head>
 
<body>
</body>
</html>

ie8还不支持indexOf和splice