Array declaration and reading and writing
-
Array literal declaration
var arr = [1.2.3.4]; Copy the code
-
Constructor declaration
var arr = new Array(1.2.3); Copy the code
-
Type conversion declaration
var arr = Array(1.2.3); Copy the code
All arrays inherit from Array.prototype
If the constructor is given a single argument num, it declares an array of num length empty, i.e. undefined
-
Poor array
var arr = [, 2.4,]; console.log(arr); // empty, 2, empty, 4 Copy the code
Array literals can be defined as sparse arrays, printing empty where no value is given, which is actually undefined. Note that the last “, “does not count
-
Arrays are based on key-value pairs of objects
var arr = [1.2.3.4.5]; var obj = { 0: 1.1: 2.2: 3.3: 4 }; Copy the code
Array overrides the toString method of Object
-
Reading and writing arrays
Use the subscript (index) to read the data in the corresponding position, and return undefined if there is none
var arr = [, 2.4,]; arr[0] = 1; Copy the code
Push () and the unshift ()
The push and unshift methods inherit from Array.prototype
Push adds one bit after the last bit of the array, and unshift adds one bit before the first
The parameter is the content to be added, which can be multiple
Returns the added length of the array
var arr = [1.2.3];
arr.push(4.5);
arr.unshift(0);
console.log(arr);
// [0, 1, 2, 3, 4, 5]
Copy the code
Pop (), and the shift ()
The pop and shift methods inherit from Array.prototype
Pop deletes the end of the array, shift deletes the first
Returns the deleted value
var arr = [1.2.3];
console.log(arr.pop());
console.log(arr);
console.log(arr.shift());
console.log(arr);
/ / 3
/ / 1. 2
/ / 1
/ / 2
Copy the code
reverse()
The reverse method inherits from Array.prototype
Inverts the contents of the array and returns the inverted array
var arr = [1.2.3];console.log(arr.reverse());/ / [3, 2, 1)
Copy the code
splice()
The splice method inherits from Array.prototype
Clipping the contents of the array, you can clipping places to add data that will affect the original array
Parameters: start subscript, cut length, data added at start subscript position
Returns the clipped subarray
var arr = [1.2.3];console.log(arr.splice(1));console.log(arr);/ / / / [1] [2, 3]
Copy the code
For the first parameter only, cut all data from the beginning subscript to the end
If it’s negative, it starts at -1 and goes backwards
var arr = [1.2.3];console.log(arr.splice(1.1));console.log(arr);/ / [2] / [1, 3]
Copy the code
Give two parameters, representing the start subscript and the shear length
var arr = [1.2.3];console.log(arr.splice(1.1.100.101));console.log(arr);// [1, 100,101, 3]
Copy the code
For the three parameters, cut and add data after the start subscript position
slice()
Like splice(), copies the contents of an array and returns it, but does not affect the original array
Parameters: start subscript, end subscript, excluding end subscript, left closed right open
var arr = [1.2.3];console.log(arr.slice(0.2));console.log(arr);// [1, 2]// [2, 3]
Copy the code
sort()
The sort method is used to sort arrays, inherited from array.prototype
The default is to sort by ASCII
Change the contents of the array and return the sorted array
```javascriptvar arr = [1, 4, 2, 3, 30]; console.log(arr.sort()); // [1, 2, 3, 30, 4]Copy the code
The sort method gives a function(a, b){} that ranks a after B if it returns a positive number; otherwise, a precedes b
var arr = [1.3.14.27.5.4];console.log(arr.sort(function(a, b) { returna - b; }));// [1, 3, 4, 5, 14, 27]
Copy the code
Math.random() returns a random number between (0, 1) and sort the array because math.random () -0.5 returns a random number between (-0.5, 0.5)
var arr = [1.2.3.4.5.6];console.log(arr.sort(function(a, b) { return Math.random() - 0.5; }));Copy the code
concat()
Used to concatenate two arrays and return the concatenated array as the array to be concatenated later
var arr1 = [1.2.3];var arr2 = [4.5.6];console.log(arr1.concat(arr2));// [1, 2, 3, 4, 5, 6]
Copy the code
toSting()
Converts an array to a string and returns the string, separated by commas
var arr = [1.2.3];console.log(arr.toString());
Copy the code
Jion (), and the split ()
-
The jion method converts an array to a string and returns the string with the argument as a delimiter
-
The split() method converts a string into an array and returns the first argument of the array as a delimiter, the second argument as the first bits, or none at all
var arr = [1.2.3]; str = arr.join(The '-');console.log(str);console.log(str.split(The '-'));console.log(str.split(The '-'.2));[1, 2] // [1, 2] Copy the code
filter()
Compatible with Internet Explorer 9 or later
Used to filter the contents of an array. The return value of the parameter method determines whether the corresponding element is left or not. If false is returned, the element is discarded
Var arr = [0, 1, 2, 2, 3, 4, 4, 0, 1]; Funtion uniqe(arr) {return arr. Filter (function(item, index, arr) { Return arr.indexof (item) === index; }); }// Method defect: cannot remove [{}, {}]
Copy the code
An array of class
-
Class arrays are similar to arrays such as Arguments, NodeList, etc. They have length attributes and even forEach attributes
-
Class arrays don’t inherit array. prototype. They don’t have push, splice, etc
-
Both the class array and the underlying array are object-based key-value pairs
-
Class arrays that do not support Array methods can be operated on by borrowing array.prototype
var obj = { 2: 3.3: 4.length: 2.push: Array.prototype.push}obj.push(1); obj.push(2);console.log(obj);// {// 2: 1,// 3: 2,// length: 4,// push: push(){}// __poroto__: {... } / /} Copy the code
Array.prototype.push (); array.prototype (); array.push ()
Array.prototype.push = function (elem) { this[this.length++] = elem; }Copy the code