给Javascript数组插入一条记录的代码
发布时间:2016-12-30 来源:查字典编辑
摘要:Array.prototype.insert=function(vVal,nIdx){vararrTemp=this;if(nIdx>arr...
<scripttype="text/javascript">
Array.prototype.insert=function(vVal,nIdx){
vararrTemp=this;
if(nIdx>arrTemp.length)nIdx=arrTemp.length;
if(nIdx<-arrTemp.length)nIdx=0;
if(nIdx<0)nIdx=arrTemp.length+nIdx;
for(varii=arrTemp.length;ii>nIdx;ii--){
arrTemp[ii]=arrTemp[ii-1];
}
arrTemp[nIdx]=vVal;
returnarrTemp;
}
vararr=["w","w","w","i","e","c","n","n","e","t"];
alert(arr.join(""));
arr.insert(".",3);
alert(arr.join(""));
arr.insert(".",-3);
alert(arr.join(""));
</script>