Home  >  Article  >  Web Front-end  >  Javascript remove custom array deletion method

Javascript remove custom array deletion method

高洛峰
高洛峰Original
2017-01-20 11:04:321535browse

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
<title>Javascript自定义数组删除方法remove()</title> 
<script type="text/javascript"> 
 Array.prototype.remove=function(dx){ 
   if(isNaN(dx)||dx>this.length){return false;} 
   for(var i=0,n=0;i<this.length;i++){ 
     if(this[i]!=this[dx]){ 
       this[n++]=this[i] 
     } 
   } 
   this.length-=1 
  } 
function f(){ 
var str = "001|002|003";// 
var arr = str.split("|"); 
alert("删除前数组长度:" + arr.length); 
for(var i=0;i<arr.length;i++){ 
alert(arr[i]); 
} 
arr.remove(1);//javascript本来是不包含些函数的。 
alert("删除后数组长度:" + arr.length); 
for(var i=0;i<arr.length;i++){ 
alert(arr[i]); 
} 
} 
</script> 
</head> 
<body> 
<input type="button" onclick="f();" value="ok" /> 
</body> 
</html>

For more articles related to Javascript remove custom array deletion method, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn