js operation method

concat () method to copy
  concat copy, add a new element to the new array after copying, a plurality of arrays are connected to form an array
According to a new array can be an array of all creation. This method will create a copy of the current array, and then add the received parameter to the end of this copy, and finally returns an array of new construction.
Passed to concat () method is one or more arrays
If the value passed is not an array, these values ​​will be simply added to the end result of the array
was arr = [1,3,5];
 var arr1=arr.concat(2,4,6,[7,8,9]);
 alert(arr1);//1,3,5,2,4,6,7,8,9
 

concat Reconstruction:

function concat(arr){
var array=[];
var index=0;
for(var i=0;i<arr.length;i++,index++){
array[index]=arr[i];
}
for(var j=1;j<arguments.length;j++,index++){
if(arguments[j].constructor===Array){
for(var k=0;k<arguments[j].length;k++,index++){
array[index]=arguments[j][k];
}
index--;
}else{
array[index]=arguments[j];
}
}
return array;
}

 
 
slice () method to copy
It can create a new array based on one or more items in the current array
slice () method can accept one or two parameters, i.e., to return to the start and end position of the item

 arr.slice (from any starting position, what position taken before); // Returns a new array, without changing the original array
 var arr1 = arr.slice (); // copy a new array
 var arr1 = arr.slice (1 ); // copy from several to tail
 var arr1 = arr.slice (-2); // copy from the second to last to the end of
 var arr1 = arr.slice (2,3); // from the second copy to third place before

eg:

 

var colors = ["red", "green", "blue", "yellow", "purple"];

 

var colors2 = colors.slice(1);

 

var colors3 = colors.slice(1,4);

 

alert(colors2); //green,blue,yellow,purple

 

alert(colors3); //green,blue,yellow

 

slice reconstruction:

function slice(arr,start,end){
start=Number(start);
end=Number(end);
if(isNaN(start)) start=0;
if(isNaN(end)) end=arr.length;
if(start<0) start=start+arr.length;
if(end<0) end=end+arr.length;
var a=[];
for(var i=start,j=0;i<end;i++,j++){
a[j]=arr[i];
}
return a;
}
var arr1=slice(arr,2,-1);
console.log(arr1,arr);

 

splice()方法    删除   插入   替换

 给数组中添加、删除、替换一个或者多个元素,返回被删除元素组成的数组

 
删除:可以删除任意数量的项,只需指定 2 个参数:要删除的第一项的位置和要删除的项数
插入:可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、0(要删除的项数)和要插入的项。如果要插入多个项,可以再传入第四、第五,以至任意多个项。
替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。

arr.splice(从什么位置开始,删除多少个元素,添加的元素...);
  var arr=[1,3,5,7,9,2,4,6,8,10];
// var arr1=arr.splice();//创建一个空数组

// var arr1=arr.splice(0);//将数组的所有元素传递给新数组

// 负数指从后向前数
// var arr1=arr.splice(-2);//从数组的倒数第二位开始截取到尾部

// var arr1=arr.splice(2,2);//从第二位开始删除2个元素

// var arr1=arr.splice(2,0,12,14);//插入元素13,14,插入在第二位

// var arr1=arr.splice(2,2,12,14);//替换元素,删除两位并且插入12,14
// console.log(arr1,arr);

splice重构:

function splice(arr,start,count){
var a=[];
start=Number(start);
count=Number(count);
if(isNaN(start)) return a;
if(start<0) start+=arr.length;
if(isNaN(count)) count=arr.length-start;
for(var i=start,j=0;i<arr.length;i++,j++){
if(j<count)a.push(arr[i]);
arr[i]=arr[i+count];
}
for(var l=0;l<arguments.length-3;l++){
for(var m=arr.length-1;m>=start+l;m--){
arr[m+1]=arr[m];
}
}
for(var n=3;n<arguments.length;n++){
arr[start+n-3]=arguments[n];
}
for(var k=0;k<count;k++){
arr.length--;
}
return a;
}
var arr1=splice(arr,-2,2,10,11,12);
console.log(arr1,arr);

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zmlAliIqsgu/p/12129457.html