In the basics of JavaScript programming, arrays are what we most often encounter, and some of the common methods of arrays are what we have to master, and here we summarize the methods commonly used in arrays .
ToString () and valueof () methods
The ToString () method, in which the array is pieced together as a string, is returned by default, separated by commas. valueof returns the array itself.
1
var a=["A3", "BS", "C1", "D Deng"];
2
var c=a.tostring ();
3
var b=a.valueof ();
4 document.write (c);
Array.concat (Item ...)
The Concat method produces a new array that contains a shallow copy of an array and attaches one or more parameters to the back.
1
var a=["A", "B", "C"];
2
var b=["D", "E", "F"];
3
var c=a.concat (b,true);
4 document.write (c);
Array.join (Separtor)
The join method is to construct an array into a string that first constructs each element in the array into a string, then joins them with a separtor delimiter, the default delimiter is comma ', '. To achieve an endless connection, you use an empty string as a separator.
1
var a=["A3", "BS", "C1", "D Deng"];
2
var c=a.join ("");
3 document.write (c)
Array.pop and Array.push (item ...)
Pop and push methods allow arrays to work like stacks, and pop methods move the last element of an array and return the element.
1
var a=["A", "B", "C"];
2
var c=a.pop ();
3 document.write (c)
The push method attaches one or more parameter item to the tail of an array. Unlike the contact method, if item is an array, it adds the parameter array as a single element to the array, modifies the array A, and returns to the length value of the array.
1
var a=["A", "B", "C"];
2
var b=["x", "Y", "z"];
3
var c=a.push (b,true);
4 document.write (c)
Array.reverse ()
The reverse method reverses the order of the elements in the array and returns the array itself
1
var a=["A", "B", "C", "F"];
2
var b=a.reverse ();
3 document.write (c);
Array.shift and Array.unshift (item ...)
The shift method shifts the first element of the divisor group and returns the pop-up element. If this array is empty, return undefined.
1
var a=["A1", "B2", "C3", "F4"];
2
var b=a.shift ();
3 document.write (c);
The Unshift method is used to insert the item into the beginning of the array rather than the tail, which returns the length of the new array
1
var a=["A1", "B2", "C3", "F4"];
2
var b=a.unshift ("@2", "? s");
3 document.write (c);
Array.slice (Start,end)
The slice method is to cut a section from an array with a length of end-start. End default length is Array.Length
1
var a=["A1", "B2", "C3", "F4"];
2
var c=a.slice (1,3);
3 document.write (c);
Array.splice (Start,deletecount,item ...)
The splice method is to remove one or more parameters from the array and replace them with the new item. Returns an array that contains the removal parameters.
1
var a=["A1", "B2", "C3", "F4"];
2
var c=a.splice (1,2, "@2", "$");
3 document.write (c);
Array.Sort (COMPAREFN)
The sort method sorts the contents of the array, and the default comparison function treats the element being sorted as a string. So you don't usually get the results you expect.
1
var a=[2,5,31,9,60,5,24,9];
2
var c=a.sort ()
3 document.write (c);
We can use our comparison function to replace the default comparison function. If we rank the numbers in ascending order, the constructor receives two arguments, and if the equality returns 0, the first argument should be in front, returning a negative number. Returns a positive number if the first argument should be in the back row.
1
var a=[2,5,31,9,60,5,24,9];
2
var c=a.sort (function (a,b) {
3 return
a-b;
4
})
5 document.write (c);