Summary of array methods (return value of array methods, whether to change the original array)

Keywords: Javascript html5

1. Summary of array methods

Method NameRole and Return Values

Is the original array changed
concat()Merge the array and return the merged datano
join()Converts an array to a string and returns it using a delimiterno
pop()Delete the last bit and return the deleted datayes
shift()Delete the first bit and return the deleted datayes
unshift()Add one or more data in the first place, return the lengthyes
push()Add one or more data to the last bit, return lengthyes
reverse()Reverse the array and return the resultyes
slice()Intercepts an array of specified locations and returnsno
sort()Sort (character rule), return resultsyes
splice()Deletes the specified location, replaces it, and returns the deleted datayes
toString()Converts directly to a string and returnsno
valueOf()Returns the original value of an array objectno
indexOf()Query and return the index of the datano
lastIndexOf()Reverse query and return index of datano
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
map()Together with forEach, the callback function returns data, forming a new array returned by mapno
filter()With forEach, the callback function returns a Boolean value, and a new array of true data is returned by filterno
every()With forEach, the callback function returns Boolean values, all true, and all trueno
some()As with forEach, the callback function also returns a Boolean value, as long as one is true and some returns trueno
reduce()Merge, with forEach, iterates over all items of the array and builds a final value that is returned by reduceno
reduceRight()Reverse merge, with forEach, iterates over all items of the array and constructs a final value, which is returned by reduceRightno

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


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


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


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


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


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


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


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



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


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.


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.


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.


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

Posted by beanman1 on Fri, 08 Oct 2021 09:17:47 -0700