1,join()
join()
Method is used to convert all elements in an array into a string. Elements are delimited by a specified delimiter.arrayObject.join(separator)
The parameter represents the separator and is optional. Default comma if no parameters are passed.Note that the return value is a string, not an array.
Note: This method does not change the original array.var arr = [1, 2, 3, 4, 7, 9, 6]; arr.join(); // "1,2,3,4,7,9,6" arr.join(" "); // "1 2 3 4 7 9 6" arr.join(""); //"1234796" var a = new Array(10); //An empty array of 10 lengths a.join("*"); //"*********"; a string of nine * symbols
The join() method is the reverse operation of the String.split() method, which splits a string into blocks to create an array.
2,sort()
The sort() method is used to sort the elements of an array and return the sorted array.
Note: This method changes the original array
When invoked without parameters, array elements are sorted by alphabet (temporarily converted to string comparisons if necessary).
//Alphabetical sorting var a = [1, 2, 3, 4, 7, 9, 6]; a.sort() //[1, 2, 3, 4, 6, 7, 9]; //Alphabetical sorting var b = [33, 222, 4, 1111, 5555]; b.sort() //[1111, 222, 33, 4, 5555] //Alphabetical sorting var c = ['c', 'ab', 'cd', 'a', 'f']; c.sort() //["a", "ab", "c", "cd", "f"] //Alphabetical order, such as capitalization, capitalization, and lowercase var d = ['a', 'Ah', 'D', 'bcc']; d.sort() //["Ah", "D", "a", "bcc"] //If the array has''elements, they will be at the head of the array. var e = ['a', 'Ah', 'D', 'bcc', '', '']; e.sort() //["", "", "Ah", "D", "a", "bcc"] //If the array contains undefined elements, they are placed at the end of the array
In order to sort arrays in other ways than alphabetical order, it is necessary to pass a comparison function to sort() as a parameter. This function determines the order of its two parameters in the array. Returns the negative number, with the first parameter in front. Conversely, return a positive number, the first parameter after. If 0 is returned, that is to say, sorting does not matter.
var b = [33, 222, 4, 1111, 5555]; b.sort() //[1111, 222, 33, 4, 5555]; alphabetical order b.sort(function(a, b) { return a - b; }); //[4, 33, 222, 1111, 5555]; numerical order var d = ['a', 'Ah', 'D', 'bcc']; d.sort() //["Ah", "D", "a", "bcc"] Case-sensitive d.sort(function(f, g) { var x = f.toLowerCase(); //All converted to lowercase var y = g.toLowerCase(); //All converted to lowercase if (x < y) return -1; if (x > y) return 1; }); //["a", "Ah", "bcc", "D"] case-insensitive
Note that it's very convenient to use anonymous function expressions here. Since the function is only used once, there is no need to name it.
3,reverse()
everse() method is used to reverse the order of elements in an array and return an array in reverse order.
Note: This method changes the original array
reverse() is an inverse version of sort(), and the detailed ordering behavior of reverse() can be explained by the sort() method above.
var b = [33, 222, 4, 1111, 5555]; b.sort(); //[1111, 222, 33, 4, 5555] Positive Sort b.reverse(); //[5555, 1111, 4, 222, 33] inverse ordering
4,push()
push() method adds one or more elements to the end of the array and returns a new length, that is, the length of the array after adding elements.
Note: This method changes the original array
arrayObject.push(newelement1, newelement2, ...., newelementX)
push()
The method must have at least one parameter. push() Method adds its parameter order toarrayObject
The tail.push()
Methods and methodspop()
Method Use the function of first-in-last-out stack provided by array.Be careful:
push()
A parameter in a method, regardless of its type (array, object, etc.), will be inserted as a whole element.arrayObject
No splitting is done at the tail of the system. See the example for details.var a = ["a","b"]; var b = { name: "Tom" }; var c = [1,2,3]; console.log(a); // ["a", "b"] console.log(a.push(b)); //3 console.log(a); /*["a", "b", [object Object] { name: "Tom" }]*/ console.log(a.push(c)); console.log(a); //4 /*["a", "b", [object Object] { name: "Tom" }, [1, 2, 3]]*/
5,pop()
The pop() method is used to delete and return the last element of the array.
Note: This method changes the original array
The pop() method changes the array (deletes the last element of the array, reduces the length of the array by 1), and returns the value of the element it deletes. If the array is empty, pop() does not change the array and returns the undefined value.
var arr = ["George", "John", "Thomas"]; console.log(arr.pop()); // "Thomas" console.log(arr); / /["George", "John"]; console.log(arr.pop()); //"John" console.log(arr); // ["George"] console.log(arr.pop()); //"George" console.log(arr); //[] console.log(arr.pop()); //undefined console.log(arr); //[]
6,unshift()
unshift() method adds one or more elements to the beginning of an array and returns a new length.
Note: This method changes the original array
arrayObject.unshift(newelement1, newelement2, ...., newelementX)
Parameter newelement1... X must have at least one. The unshift() method inserts its parameters into the head of the arrayObject and moves the existing elements sequentially to the higher subscripts to make room. The first parameter of this method will be the new element 0 of the array, and if there is a second parameter, it will be the new element 1, and so on.
In IE6 and IE7, unshift() returns underfined!
var arr = ["George", "John", "Thomas"]; console.log(arr.unshift("William")); //4 console.log(arr.unshift("Tom", "Jerry")); //6 console.log(arr); //["Tom", "Jerry", "William", "George", "John", "Thomas"]
7,shift()
The shift() method is used to delete the first element of the array and return the value of the first element.
If the array is empty, the shift() method does nothing and returns the undefined value.
Note: This method changes the original array
var arr = ["George", "John", "Thomas"]; console.log(arr.shift()); //"George" console.log(arr); //["John", "Thomas"] console.log(arr.shift()); //"John" console.log(arr); //["Thomas"] console.log(arr.shift()); //"Thomas" console.log(arr); //[] console.log(arr.shift()); //undefined
8,contact()
The concat() method is used to connect two or more arrays.
This method does not change the existing array, but only returns a copy of the connected array.
Note: Parameters are required. It can be either a specific value or an array object, which can be any number. If the parameter is an element, splice the element. If the parameter is an array, then the elements in the splice array are not the array itself.
var a = [1,2,3]; var b = a.concat(4,5); var c = a.concat(4,5,[6,7],8,"123"); console.log(a); //[1, 2, 3] console.log(b); //[1, 2, 3, 4, 5] console.log(c); //[1, 2, 3, 4, 5, 6, 7, 8, "123"]
9,slice()
slice()
Method can return selected elements, a fragment or a subarray from an existing array.Note that this method does not modify the array, and it returns a new array containing start reach end (Excluding this element) arrayObject Elements in.
arrayObject.slice(start, end)
parameter start It is necessary to specify where to start selecting and, if negative, where to start at the end of the array. In other words,-1 Referring to the last element,-2 Point to the penultimate element, and so on.parameter end It is optional to specify where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the sharded array contains the following start All elements to the end of the array. If this parameter is a negative number, it specifies elements that start at the end of the array.
in use slice()If the parameter start Beyond the starting point of the array, it starts at the head of the array; if the parameter end Beyond the end of the array, it ends at the end of the array; if start and end The range in the array is not in the array, or end less than start,An empty array is returned; if start and end If the number is not enough, the conversion will be carried out. If the conversion fails, start The default is 0, end The default is 0. See examples for details:
var arr = ["George", "John", "Thomas", "James", "Adrew", "Martin"]; console.log(arr.slice(2,4)); //["Thomas", "James"] console.log(arr.slice(-3,4)); //["James"] console.log(arr.slice(-10,4)); //["George", "John", "Thomas", "James"] console.log(arr.slice(-10,-4)); //["George", "John"] console.log(arr.slice(4,3)); //[] console.log(arr.slice(-20,-10)); //[] console.log(arr.slice("2","4")); //["Thomas", "James"] console.log(arr.slice("a","4")); //["George", "John", "Thomas", "James"] console.log(arr.slice("a","b")); //[] console.log(arr.slice("2a","4a"));//[] console.log(arr.slice("","")); //[]
10,splice()
The splice() method is used to insert, delete, or replace elements of an array.
Note: splice() modifies the original array directly.
arrayObject.splice(index, howmany, element1, ....., elementX)
The parameter index is required. It specifies where to add/delete elements. This parameter is the subscript of the array elements that start (include) insert and/or delete. It must be a number.
The parameter howmany is required. Provide how many elements should be deleted. It must be a number, but it can be "0". If this parameter is not specified, all elements from index to the end of the original array are deleted.
Parameter element1... elementX is optional. Specify new elements to be added to the array, starting at the subscript indicated by index.
splice() can delete zero or more elements starting at index and replace those deleted elements with one or more values declared in the parameter list. If the element is deleted from arrayObject, an array containing the deleted element is returned.
Note the difference between slice() and splice(): slice means to slice an array out of a section; splice means to twist, if you connect two ropes, you need to cut the original rope, and then splice the new rope with the two segments of the old rope that have been cut together. If you want to delete an element in an array and add a new element to the array, you should use the method Array.splice().
Note that if the parameter index is not a number, it will be automatically converted, as shown in the example:
var arr = ["George", "John", "Thomas", "James", "Adrew", "Martin"] console.log(arr.splice(2, 1)); //["Thomas"] console.log(arr); //["George", "John", "James", "Adrew", "Martin"] console.log(arr.splice(2, 2, "William")); //["James", "Adrew"] console.log(arr); //["George", "John", "William", "Martin"] console.log(arr.splice(2, 1, "Tom", "Jerry")); //["William"] console.log(arr); //["George", "John", "Tom", "Jerry", "Martin"] console.log(arr.splice(2)); //["Tom", "Jerry", "Martin"] console.log(arr); //["George", "John"] console.log(arr.splice("2")); //[] console.log(arr); //["George", "John"] console.log(arr.splice("a")); //["George", "John"] console.log(arr); //[]
11. toString() and toLocaleString()
The toString() method converts an array to a string and returns the result.
Array.toString() is equivalent to Array.join(), and the return value is the same as the string returned by the join() method without parameters.
toLocaleString() converts an array to a local string. The local string representation of the return value Array.
Methods to change the original array: pop(), push(), reverse(), shift(), sort(), splice(), unshift()
Methods that do not change the original array: concat(), join(), slice(), toString(), toLocaleString()
Note: In JavaScript, there is no good mechanism to distinguish Array from Object, and arrays can generally be identified by the following methods:
var isArray = function(value){ return Object.prototype.toString.apply(value) === '[object Array]'; }
Object.prototype.toString always returns a string "[object, class]" for any variable, and this class is the name of the constructor of the JavaScript embedded object. As for user-defined variables, class is equal to object.
Therefore, variable data types can be accurately obtained by Object.prototype.toString.apply(obj).
Data types available through Object.prototype.toString include: Date,Object,String,Number,Boolean,Regexp,Function,undefined,null,Math, etc.
ECMAScript 5 defines nine new array methods and has the opportunity to write again!
Reference to the above: [Feeldesign Studio ](http://www.feeldesignstudio.c... And JavaScript Authoritative Guide
Thank you for watching. I hope Daniel will guide us and Mavericks will make progress together.~