First, insert
1. Sequential insertion
1 functionInsert (arr, n, data) {2 3 //If the insertion data location is not in the footer4 if(N <arr.length)5 {6 //after the position is inserted, the element moves backward one7 for(vari = seqlist.listlen-1; I >= N; i--)8 {9Seqlist.listdata[i + 1] =Seqlist.listdata[i];Ten } One } A //inserts data into position n position and adds 1 to the length of the array -Seqlist.listdata[n-1] =data; -seqlist.listlen++; the return true; -}
Second, delete
1. Prototyping methods
//adds a prototype method to the array local object, which is used to delete duplicate entries in the array entry (there may be multiple), and the return value is a new array containing the duplicate entries that were deleted. //The first method------prototype----Array.prototype.distinct =function(){ varArr=[]; varobj={};//object to undertake for(vari=0,len= This. length;i<len;i++){ if(!obj[ This[i]]) {obj[ This[I]] =1; Arr.push ( This[i]); } } returnarr; }; vararr =NewArray (); Arr.push (1,3,2,4,4,4,5,6,7,8,8,8,9,0); Console.log (arr); Console.log (Arr.distinct ());
The second method-----prototype
Array.prototype.distinct = function () {
var arr=[];
for (Var i=0,len=this.length;i<len;i++) {
if (This[i]==this[i+1]) {
Continue
}else{
Arr.push (This[i]);
}
}
return arr;
};
var arr = new Array ();
Arr.push (1,3,2,4,4,4,5,6,7,8,8,8,9,0);
Console.log (arr);
Console.log (Arr.distinct ());
Slice (start,end) returns a cropped array of elements
Console.log (Arr.slice (2,4));
Console.log (arr);
The splice () method is different from the slice () method, and the splice () method modifies the array directly
Arr.splice (0,2,80);
Console.log (arr);
The third method-----before and after all elements are compared
Array.prototype.distinct = function () {
for (Var i=0,len=this.length;i<len;i++) {
for (Var j=i+1;j<=len;j++) {
if (This[i]==this[j]) {
Console.log (This[j]);
This.splice (j,1);
j--; Consider that if you delete an element, the value of J needs to be reduced by 1
len--;
}
}
}
return this;
};
var arr = new Array ();
Arr.push ("A", "B", "C", "C", "B", "BB", "DD", "D", "AA", "C", "BB");
var cc = Arr.splice (5,1) [0];
Console.log (arr);
Console.log (CC);
Arr.distinct ()
Console.log (arr);
2. General functions
functionRemoverepeat (arr) {vararray=[]; varobj={}; for(vari=0,len=arr.length;i<len;i++) {if(!Obj[arr[i]]) {Obj[arr[i]]=1; Array.push (Arr[i]); }}returnArray; }; vararr =NewArray (1,3,2,4,4,4,5,6,7,8,8,8,9,0); varArray =removerepeat (arr); Console.log ("Removerepeat:" +array); Console.log (arr);
Third, increase
Four, find
Five, sort
/*//-------------bubble sort (from small to Large)-------------for (Var i=0,len=arr.length;i<len;i++) {for (Var j=i+1;j<len;j++) {if (Arr[i]>arr[j]) {//will put the largest in the last Var temp=0; T=arr[i]; ARR[I]=ARR[J]; Arr[j]=temp; }}}//--------the second (improved)------------for (Var i=1,len=arr.length;i&l t;len;i++) {for (Var j=0;j<len-i;j++) {//The largest array already sorted does not have to compare if (Arr[j]>arr[j+1]) { var temp=0; T=arr[i]; ARR[I]=ARR[J]; Arr[j]=temp; } } } */ /*//-------------Select Sort (from small to Large)-------------for (var i=0,len=arr.length-1;i<len;i++) {int m in = i; for (Var j=i+1;j<len;j++) {if (Arr[min]>arr[j]) {min=j; }} if (Min!=i) {var temp=0; T=arr[min]; Arr[min]=arr[i]; Arr[i]=temp; } }*/ /*//-------------Direct Insert sort (from small to Large)-------------for (int i = 1; i < arr. Length; i++) {if (arr[i-1] > Arr[i]) {int temp = Arr[i]; Each time you remove the current value to compare int j = i; while (J > 0 && arr[j-1] > Temp)//greater than it, assign to it {arr[j] = Arr[j -1]; j--; } Arr[j] = temp; } }*/ /*//-------------js for fast sorting (small to Large)-------------*/ /*-----------------First---------------var quickSort = function (arr) {var arr = arr.concat (); New usage of/concat (deep copy) if (arr.length<=1) return arr; var index = Math.floor (ARR.LENGTH/2); var centervalue = Arr.splice (index,1); Console.log (Centervalue); var left = []; var right = []; for (Var i=0,len=arr.length;i<len;i++) {if (Centervalue>=arr[i]) {Left.push ( Arr[i]); }else{Right.push (Arr[i]); }}//Debugger//console.log (QuickSort (left)); Console.log (right); var res1 = Arguments.callee (left); var res2 = Arguments.callee (right); Return Left.concat (right); Return Res1.concat (CENTERVALUE,RES2); } var arr=[9,8,7,4, 5,3,77]; var result = QuickSort (arr); Console.log (result); Console.log (arr);*/ //-----------------Second--------------- varQuickSort =function(arr) {if(arr.length<=1)returnarr; varindex = Math.floor (ARR.LENGTH/2); Console.log (index);//1 varCentervalue = Arr.slice (index,index+1) [0]; Console.log (Centervalue); //5 varleft = []; varright = []; for(vari=0,len=arr.length;i<len;i++){ if(centervalue>=Arr[i]) {Left.push (arr[i]); }Else{Right.push (arr[i]); } } varRes1 =Arguments.callee (left); varRes2 =Arguments.callee (right); returnRes1.concat (Res2); } vararr=[9,8,7,4,5,3,4,77]; //var result = QuickSort (arr); //Console.log (result);Console.log (arr); varaa=[3,4]; //Console.log (aa.slice);Console.log (Aa.splice (0,0)); /*//-------------Reverse sort-----------------for (Var i=0,len=arr.length-1;i<len/2;i++) {var Temp=0; T=arr[i]; Arr[i]=arr[len-1-i]; Arr[len-1-i]=temp; }*/ /*//-------------parameter sorting (from small to Large)-----------------function Mysort () {var tags = new Array ();//Use Array as a parameter storage container tags = Array.prototype.slice.call (arguments); Tags.sort (function (A, a,) {return a-B; }) Return tags;//returns the sorted array} var result = Mysort (50,11,16,32,24,99,57,10 0); The number of incoming parameters is indeterminate console.info (result); Show Results*/
JS array additions and deletions check and change