1. Summary of array methods
Method Name | Role and Return Values | Is the original array changed |
---|---|---|
concat() | Merge the array and return the merged data | no |
join() | Converts an array to a string and returns it using a delimiter | no |
pop() | Delete the last bit and return the deleted data | yes |
shift() | Delete the first bit and return the deleted data | yes |
unshift() | Add one or more data in the first place, return the length | yes |
push() | Add one or more data to the last bit, return length | yes |
reverse() | Reverse the array and return the result | yes |
slice() | Intercepts an array of specified locations and returns | no |
sort() | Sort (character rule), return results | yes |
splice() | Deletes the specified location, replaces it, and returns the deleted data | yes |
toString() | Converts directly to a string and returns | no |
valueOf() | Returns the original value of an array object | no |
indexOf() | Query and return the index of the data | no |
lastIndexOf() | Reverse query and return index of data | no |
forEach() | The parameter is a callback function, which traverses all items of the array. The callback function accepts three parameters, value, index, self; forEach does not return a value. | no |
map() | Together with forEach, the callback function returns data, forming a new array returned by map | no |
filter() | With forEach, the callback function returns a Boolean value, and a new array of true data is returned by filter | no |
every() | With forEach, the callback function returns Boolean values, all true, and all true | no |
some() | As with forEach, the callback function also returns a Boolean value, as long as one is true and some returns true | no |
reduce() | Merge, with forEach, iterates over all items of the array and builds a final value that is returned by reduce | no |
reduceRight() | Reverse merge, with forEach, iterates over all items of the array and constructs a final value, which is returned by reduceRight | no |
2. Introduction of Array Methods
1. concat() Joins elements into an array
var str1 = "Hello "; var str2 = "world!"; var n = str1.concat(str2);//Hello world!Output Results
2.join()
Places all elements in the array into a string based on the specified delimiter and returns the string.
Parameter: join(str);The parameter is optional, defaulting to the "," sign, with the incoming character as the delimiter.
var arr = [1,2,3]; console.log(arr.join()); //1,2,3 console.log(arr.join("-")); //1-2-3 console.log(arr); //[1,2,3] - original array unchanged
3.pop()
The method deletes and returns the last element of the array.
var arr = [1,2,3]; console.log(arr.pop()); //3 console.log(arr); //[1,2] - original array change
4.shift()
Method is used to delete and return the first element of the array.
var arr = [1,2,3] console.log(arr.shift()); //1 console.log(arr); //[2,3] - original array change
5.unshift()
Add one or more elements to the beginning of the array and return the new length.
Parameter: unshift(newData1, newData2,...)
var arr = [1,2,3]; console.log(arr.unshift("hello")); //4 console.log(arr); //['hello', 1,2,3] - original array change console.log(arr.unshift("a","b")); //6 console.log(arr); //['a','b','hello', 1,2,3] - original array change
6.push()
Add one or more elements to the end of the array and return the new length.
Parameters: push(newData1, newData2,...)
var arr = [1,2,3]; console.log(arr.push("hello")); //4 console.log(arr); //[1,2,3,'hello']--original array change console.log(arr.push("a","b")); //6 console.log(arr); //[1,2,3,'hello','a','b'] - original array change
7.reverse()
Reverse the order of the elements in the array.
var arr = [1,2,3]; console.log(arr.reverse()); //[3,2,1] console.log(arr); //[3,2,1] - original array change
8.slice()
This method receives two parameters slice(start,end), strat is required to indicate from which bit to start, end is optional, to the first bit to end (excluding end bits), and ellipsis is to the last bit. Both start and end can be negative, and negative numbers can start from the last bit, such as -1 for the last bit.
Parameter: slice(startIndex, endIndex)
var arr = ["Tom","Jack","Lucy","Lily","May"]; console.log(arr.slice(1,3)); //["Jack","Lucy"] console.log(arr.slice(1)); //["Jack","Lucy","Lily","May"] console.log(arr.slice(-4,-1)); //["Jack","Lucy","Lily"] console.log(arr.slice(-2)); //["Lily","May"] console.log(arr.slice(1,-2)); //["Jack","Lucy"] console.log(arr); //['Tom','Jack','Lucy','Lily','May'] - The original array has not changed
9.sort()
Sorts the elements in an array, in ascending alphabetical order by default. Sorts by character encoding.
Parameter: sort(callback)
If you need to sort by numeric value, you need to pass a parameter. sort(callback), callback is a callback function.
function fn(a,b){ return a-b; } var arr = [6,1024,52,256,369]; console.log(arr.sort(fn)); //[6, 52, 256, 369, 1024] console.log(arr); //[6, 52, 256, 369, 1024] - original array change
10.splice()
Add to, remove from, or replace elements in an array, and then return the deleted/replaced elements.
Parameters: splice(start,num,data1,data2,...);start denotes the starting index position, num denotes the number of deletions, data1, data2... denotes the element to be replaced, and the parameters are optional.
11.toString()
Converts to a string, similar to join() without parameters. This method is called automatically when an implicit type conversion occurs on the data, or directly to a string if called manually.
12.valueOf()
Returns the original value of an array (usually the array itself), which is typically called in the background by js and does not appear explicitly in the code.
13.indexOf()
Based on the specified data, from left to right, the query appears in the array and returns -1 if the specified data does not exist. This method is a query method and will not change the array.
Parameter: indexOf(value, start);value is the data to be queried; start is optional, indicating where to start the query, moving forward from the end of the array when start is negative; if the query does not exist, the method returns -1.
var arr = ["h","e","l","l","o"]; console.log(arr.indexOf("l")); //2 console.log(arr.indexOf("l",3)); //3 console.log(arr.indexOf("l",4)); //-1 console.log(arr.indexOf("l",-1)); //-1 console.log(arr.indexOf("l",-3)); //2