1. means of definition;
var arr = []; Array literal
var arr = new Array (); Array constructors
Array all methods are derived from Array.prototype
2. Reading and writing of arrays;
Arr[num]//Can not overflow read results undefined
Arr[num] = xxx; Can overflow write
3. Change the original array
Push ()
Add data to the last digit of the array, the return value is the length of the array;
To simulate an array method:
Array.prototype.push = function () {
for (Var i=0;i<arguments.lenght;i++) {
This[this.length] = arguments[i];
}
return this.length;
}
Pop ()
The last digit of the cut array, the return value is the current cut value;
Shift ()
Cut the first bit of the array, the return value is the current cut value;
Unshift ()
Add data at the top of the array, and the return value is the length of the array;
Sort (FN)
Sort
1. Two parameters must be written
2. See return value 1) when the return value is negative, the preceding number is placed in front
2) is a positive number, and the following numbers are placed in front
3) is 0, do not move
Arr.sort (function (A, b) {
return a-B;
});
Reverse ()
Reverse (Reversed) Order
Splice (starting from the first, intercept the length, add new data at the incision)
Can be added, deleted, changed. The return value is a clipped value
4. Do not change the original array
Concat ()
Link array arr.concat (ARR1); The return value is a new array;
Join ()
The argument must be a string, convert the array to a string, and link to the parameter.
ToString ()
Convert an array to a string
Slice (intercept from this bit, intercept to that bit);
Intercept
# # # class Array
1. property to be an indexed (numeric) attribute, must have length
2. Does not have the method that the array has
You can add array methods to an array of classes, such as Push:Array.prototype.push
If you add the splice method, it will be the same as the number leader [1, 2, 3]
var obj = {
"0": ' A ',
"1": ' B ',
"2 ': ' C ',
"Length": 3,
"Push": Array.prototype.push,
"Splice": Array.prototype.splice
}
Benefit: (class array push is replaced by length)
Array.prototype.push = function (target) {
Obj[obj.length] = target;
Obj.length + +;
}
Record Array manipulation in JavaScript