1. join()
join('parameter') converts the elements of the array into strings with the passed parameters as the splitter.
let arr = [1,2,3,4,5]; let str = (','); (str) // -> '1,2,3,4,5';
() and pop()
push(): You can receive any number of parameters, add them one by one to the end of the array, and return the length of the modified array.
pop(): Remove the last item at the end of the array, reduce the length value of the array, and then return the removed item.
let arr = ['Zhang San','Li Si','Wang Wu']; let count = ('Maliu'); (arr) // -> ['Zhang San','Li Si','Wang Wu','Ma Liu'](count) // -> 4 let item = (); (item) // -> Maliu;
() and unshift()
shift(): Delete the first item of the original array and return the value of the deleted element; if the array is empty, return undefined.
unshift: Add the argument to the beginning of the original array and return the length of the array.
let arr = ['Zhang San','Li Si','Wang Wu']; let item = (); (arr) // -> ['Li Si','Wang Wu'](item); // -> Zhang San let count = ('Maliu'); (arr) // -> ['Ma Liu','Li Si','Wang Wu'](count) // -> 3
();
Invert the data of the array and return the inverted array, it will change the original array
let arr = [1,2,3,4,5]; let arr1 = (); (arr1) // -> [5,4,3,2,1] (arr) // -> [5,4,3,2,1]
();
Sort the data in the array (default is ascending order), and return a new array sorted, which will change the original array
let arr = [12,2,43,5,2,5]; (()) // -> [12, 2, 2, 43, 5, 5] // Note: Through the above case, you will find that the comparison between the printed array and the original array still changes [12,2,43,5,2,5] -> [12, 2, 2, 43, 5, 5]; but has the result we want achieved, why is this?// Because sorting is for character sorting, first use the toString() method of the array to convert it to a string, and then compare bit by bit. 3 is greater than 12, because the first bit is 3>1, so don't be confused with the Number type data sorting.5.1What if you need numerical sorting? // If numerical sorting is required, sort(callback) needs to pass in a callback culvert. The function should have two parameters, compare these two parameters, and then return a number (a-b) that illustrates the relative order of these two values;For example: let arr = [12,2,43,5,2,5]; (((a,b)=>a-b)) // -> [2, 2, 5, 5, 12, 43]
();
Intercepts an array at the specified position and returns the intercepted array, without changing the original array
// Note: slice (startIndex, endIndex) can have two parameters. startIndex is required, indicating which bit starts from; endIndex is optional, indicating which bit end (excluding the endIndex bit), omitting to the last bit; startIndex and endIndex can both be negative numbers. When negative numbers are counted from the last bit, such as -1 means the last bit.let arr = ['Zhang San','Li Si','Wang Wu','Maliu']; ((1,3)); // -> ['Li Si', 'Wang Wu'](arr) // -> ['Zhang San','Li Si','Wang Wu','Ma Liu']; The original array has not changed.
();
Add to, or delete from, or replace elements in the array, and return the elements that were deleted/replaced.
// Note: splice(start,num,val1,val2,...); All parameters are optional. Compared with slice, splice will change the original array.// start is the starting position, which can be a negative number. -1 means starting from the last bit, num means the length to be deleted or replaced, and cannot be a negative number.let arr = ['Zhang San','Li Si','Wang Wu','Maliu']; ((2,1)) // -> ['Wang Wu'](arr) // -> ['Zhang San','Li Si','Ma Liu'] let arr = ['Zhang San','Li Si','Wang Wu','Maliu']; ((2,1,'Qiro')) // -> ['Wang Wu'](arr) // -> ['Zhang San', 'Li Si', 'Qilang', 'Ma Liu']
();
Convert an array to a string, similar to join() without parameters. This method will be automatically called when the data is implicitly converted. If called manually, it will be converted directly into a string. Will not change the original array
let arr = [1,2,3,4,5,6]; (()) // -> '1,2,3,4,5,6' // Note: No parameters.
();
According to the specified data, from left to right, query the location in the array. If the specified data does not exist, return -1, and the specified data is found and the index returned to the data is returned.
// Note: indexOf(value, start); value is the data to be queryed; start is optional, indicating the location where the query starts. When start is a negative number, it goes forward from the tail of the array; if the query cannot exist, the method returns -1 let arr = ['Zhang San','Li Si','Wang Wu','Maliu']; (('Li Si')) // -> 1 (('Li Si',2)) // -> -1 () ES5New methods,Used to traverse arrays,No return value, // Note: forEach(callback); callback has three parameters by default, namely value (data of the array traversed), index (corresponding index), and self (array itself).let arr = ['Zhang San','Li Si','Wang Wu','Maliu'] let a = ((item,index,self)=>{ (value + "--" + index + "--" + (arr === self)); }) // The print result is:// Zhang San--0--true// Li Si--1--true// Wang Wu--2--true// Maliu--3-true(a); // -> undefined---forEach does not return a value//This method is a traversal method and will not modify the original array
();
1. Same as forEach function;
The callback function will return the execution result, and finally map will return the return values of all callback functions into a new array.
//Note: map(callback); callback has three parameters by default, namely value, index, and self. The same as the forEach() parameter abovelet arr = ['Zhang San','Li Si','Wang Wu','Maliu']; let arr1 = (item => { return 'Hello:'+item }) (arr1) // -> ['Hello: Zhang San', 'Hello: Li Si', 'Hello: Wang Wu', 'Hello: Ma Liu']
();
1. The callback function of the same forEach function needs to return a Boolean value. When true, the data of this array is returned to the filter. Finally, the filter will form a new array to return the return values of all callback functions (this function can be understood as "filtering").
// Note: filter(callback); callback has three parameters by default, namely value, index, and self.let arr = [1,2,3,4,5,6]; let arr1 = ((value,index,self)=>{ (item) // -> 1,2,3,4,5,6 (index) // -> 0,1,2,3,4,5 (self) // -> [1,2,3,4,5,6] return item > 3 }) (arr1) // -> [4,5,6]
();
Loop of the array, find the value that meets the criteria and interrupt the loop to return the found value;
let arr = ['Zhang San','Li Si','Wang Wu','Maliu']; let str = (item => item == 'Li Si'); (str); // -> 'Li Si'
();
Loop of array, find the index that meets the criteria and break the loop to return the found index value
let arr = ['Zhang San','Li Si','Wang Wu','Maliu']; let index = (item => item == 'Li Si'); (index); // -> 1;
();
Determine whether each item in the array meets the condition. Only when all items meet the condition will true be returned.
// Note: every() receives a callback function as a parameter. This callback function needs to have a return value. Every(callback); callback has three parameters by default, namely value, index, and self.let arr = [1,2,3,4,5,6]; let bool = (item => item > 0); (bool); // -> true; let bool = (item => item > 3); (bool); // -> false;
();
Determine whether there are items in the array that meet the conditions. As long as one of them meets the conditions, it will return true. The no side will return false
//Note: some() receives a callback function as a parameter. This callback function needs to have a return value. Some(callback); callback has three parameters by default, namely value, index, and self.let arr = [1,2,3,4,5,6]; let bool = (item => item > 3); (bool) // -> true; let bool = (item => item > 6); (bool) // -> false;
();
The first item of the array starts, iterates over all items of the array one by one, and then builds a final returned value.
// Note: Parameters: reduce() receives one or two parameters: the first is a callback function, which represents the function called on each item in the array; the second parameter (optional) is used as the initial value of the merge, and is received by the first parameter when the callback function is executed for the first time. reduce(callback, initial); callback has four parameters by default, namely prev, now, index, and self. Any value returned by callback will be used as the first parameter for the next execution. If the initial parameter is omitted, the first iteration occurs on the second term of the array, so the first parameter of the callback is the first term of the array, and the second parameter is the second term of the array. let arr = [10,20,30,40,50]; let sum = ((prev,now) => prev+now) (sum); // -> 150; let sum = ((prev,now) => prev+now,110) (sum)
This article explains to you the summary and example demonstration of 17 commonly used vue array methods, including: VUE array conversion string, VUE array traversal, VUE array filtering, VUE array query and other functions. For more information about VUE array operations, please check the following related links