Property name | describe |
---|---|
prototype | Adding attributes and methods to objects |
constructor | Returns an array object reference |
length | Returns the number of array elements |
Method name | describe | Return | Change the original array |
---|---|---|---|
concat() | Connect multiple arrays | New array after connection | N |
join('') | Combines all elements in an array into a string. Delimitation by delimiter | New array after merging | N |
toString() | Converting an array to a string (the same as a nonparametric join, comma join) | Converted string | N |
pop() | Delete the last element of the array (top of the stack) | Deleted element values | Y |
push() | Add one/more elements to the end of the array | New array length | Y |
shift() | Delete the first element of the array | Deleted element values | Y |
unshift() | Add one/more elements to the beginning of an array | New array length | Y |
reverse() | Reverse the order of elements in an array | Post-inverted array | Y |
slice(start,end) | Intercept subarrays from start to end (end is omitted at the end of the array) | Intercept subarray | N |
splice() | (start,length,item1,item2,...) Delete elements and add new elements | Delete subarrays | Y |
sort() | Sort the elements of an array (self-defining rules) | Post-sorted arrays | Y |
valueOf() | Returns the original value of the Array object | Array object | N |
Create Array objects
new Array(); new Array(size); new Array(element0, element1, ..., elementn); var arr = []; //Literary Quantity
It takes about the same amount of time as new Array() in the literal way. The difference in usage habits is that ~ new Array() can initialize the length.~
attribute
prototype adds attributes and methods to objects
function em(A,B) { this.A = A; this.B = B; } var e = new em("AA","BB"); em.prototype.C = null; e.C = 20000; console.log(e.C); //20000 console.log(e); //em {A:"AA" , B:"BB" , C : 20000 }
constructor returns a reference to the array function that created this object
function employee(name,job) { this.name = name; this.job = job; } var bill = new employee("Bill"); console.log(typeof bill.constructor); // function console.log(bill.constructor); //function employee(name){this.name=name;this.job=job;}
Method
concat() connects two or more arrays (returns a copy of the connected array)
var arr = [1, 2, 3]; var arr1 = [6, 7]; console.log(arr.concat(4, 5, arr1)); //[1, 2, 3, 4, 5, 6, 7]
join() puts all elements of an array into a string, separated by a specified delimiter.
var arr = ['item 1', 'item 2', 'item 3']; var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; //'<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>'
toString() array is converted to a string (the same as the string returned by the nonparametric join() method)
var arr = new Array(3) arr[0] = "A" arr[1] = "B" arr[2] = "C" console.log(arr.toString()); //"A,B,C"
pop() deletes the last element of the array, decreases the length of the array by 1, and returns the value of the deleted element
If the array is empty, pop() returns undefined value
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.pop()); //"C" console.log(arr); //["A", "B"]
push() adds one or more elements to the end of the array and returns a new length
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.push('D')); //4 console.log(arr); ////["A", "B", "C" , "D"]
reverse() reverses the order of elements in an array
var arr = ["A", "B", "C"]; arr.reverse(); console.log(arr);// ["C", "B", "A"]
slice(start,end) returns the selected subarray from the array
var arr = ["A", "B", "C"]; // From the first element to the end of the array console.log(arr.slice(1)); //["B", "C"] console.log(arr); //["A", "B", "C"]
splice() deletes elements and adds new elements to the array
(Operating directly on arrays)
(1) Delete the array elements of the specified range:
var arr = new Array(6); arr[0] = "A"; arr[1] = "B"; arr[2] = "C"; arr[3] = "D"; arr[4] = "E"; arr[5] = "F"; console.log(arr); //["A", "B", "C", "D", "E", "F"] // Three array elements after deleting the third element (including the third element) console.log(arr.splice(2, 3)); //["C", "D", "E"] console.log(arr); //["A", "B", "F"]
(2) Insert the specified element from the specified subscript (the number of elements is unlimited):
var arr = new Array(6); arr[0] = "A"; arr[1] = "B"; arr[2] = "C"; arr[3] = "D"; arr[4] = "E"; arr[5] = "F"; console.log(arr); //["A", "B", "C", "D", "E", "F"] console.log(arr.splice(2, 0, "AA","BB")); // [] console.log(arr);//["A", "B", "AA" , "BB" , "C", "D", "E", "F"]
(3) Delete the array elements of the specified range and replace them with the specified elements (the number of elements is unlimited):
var arr = new Array(6); arr[0] = "A"; arr[1] = "B"; arr[2] = "C"; arr[3] = "D"; arr[4] = "E"; arr[5] = "F"; console.log(arr);//["A", "B", "C", "D", "E", "F"] console.log(arr.splice(2, 1, "AA","BB")); // ["C"] console.log(arr); //["A", "B", "AA", "BB", "D", "E", "F"]
sort() sorts the elements of an array
Reference to arrays
Arrays are sorted on the original array and no replicas are generated. By default, this method is sorted in the order of character encoding (ASCII).
var arr = new Array(3); arr[0] = "D"; arr[1] = "S"; arr[2] = "A"; console.log(arr); //["D", "S", "A"] console.log(arr.sort()); //["A", "D", "S"]
Specify your own sort rules. As follows:
var arr = new Array(6); arr[0] = 10; arr[1] = 5; arr[2] = 40; arr[3] = 25; arr[4] = 1000; arr[5] = 1; console.log(arr); //[10, 5, 40, 25, 1000, 1] //Ascending order console.log(arr.sort(function (a, b) {return a - b;}));// [1, 5, 10, 25, 40, 1000]
valueOf() returns the corresponding original value
var arr = new Array(2); arr[0] = 10 arr[1] = 5 console.log(arr); //[10, 5] console.log(typeof arr.valueOf()); //object console.log(arr.valueOf()); //[10, 5]
shift() deletes and returns the first element of the array
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.shift()); //A console.log(arr); //["B", "C"]
unshift() adds one or more elements to the beginning of an array and returns a new length
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.unshift("AA")); //4 console.log(arr);//["AA", "A", "B", "C"]