SoFunction
Updated on 2025-03-09

A detailed explanation of the 27 methods in the javascript array

()

The concat() method is used to concatenate two or more arrays. The method does not change the existing array, but returns a new array containing the values ​​of the connected array.

var str1 = [1,2,3,4,5,6]
var str2 = ['a','b','c','d','e']
((str2));// [1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 'e']

()

copyWithin() Copy part of the data of the array overwrites another location in the array. The array will be modified, but the length of the array will not be modified.

var str1 = [1,2,3,4,5,6]
// Start copying from index 0, and ending index 1 (excluding end position), replace to the position with index 2,((2,0,1));    //[1, 2, 1, 4, 5, 6]
// Because one parameter is omitted, copy and replace all elements after index is 3, starting from index is 1.((1,3));    //[1, 4, 5, 6, 5, 6]

()

Returns an array whose elements are arrays corresponding to the enumerable attribute key-value pairs found directly on the object

var str3 = { 100: 'a', 2: 'b', 7: 'c' };
 ((str3));  //['2', 'b']['7', 'c']['100', 'a']

()

The every() method is used to determine whether all elements of the array meet the specified conditions.

var str1 = [1,2,3,4,5,6]
// Create a function as a judgment conditionfunction checkAdult(value,index) {
     return value > 0;
 }
// The every() method is used to determine whether all elements of the array meet the specified conditions.((checkAdult));    //true

()

The fill() method is used to replace all elements of the array with static elements.

var str1 = [1,2,3,4,5,6]
((1));  //[1, 1, 1, 1, 1, 1]

()

It is used to filter out certain elements of Array and then return the remaining elements.

var str1 = [1,2,3,4,5,6]
((function(x){return x > 2}));    //[3, 4, 5, 6]

() and findIndex ()

find() returns the value of the first element in the array that passes the test.

findIndex() returns the index of the first element in the array that passed the test.

var str1 = [1,2,3,4,5,6]
((function(x){return x > 2}));  //3
((function(x){return x > 2}));  //2

()

forEach() calls the function for each array element.

var str1 = [1,2,3,4,5,6]
(x => (x));    //Output in turn123456

()

Create an array as an object.

(('string'));  //['s', 't', 'r', 'i', 'n', 'g']

()

include() checks whether the array contains the specified element. The second parameter can specify where to start the detection

var str1 = [1,2,3,4,5,6]
((2,0));  //true

() and lastIndexOf()

indexOf() searches for elements in an array and returns their index. The second parameter can specify where to start the search

lastIndexOf() starts searching for elements at the end of the array and returns their index. The second parameter can specify where to start the search

var str1 = [1,2,3,4,5,6]
((2,0));  //1
((2,5));     //1

()

Can be used to determine whether it is an array object

var str1 = [1,2,3,4,5,6]
((str1));  //true

()

Concatenate all elements of the array into a string. The elements will be separated by the specified delimiter. The default delimiter is a comma (,).

var str2 = ['a','b','c','d','e']
const a = ()
(a);         //a,b,c,d,e
const a = ('')
(a);         //abcde

()

Return an array, the properties in the array are the keys corresponding to the array

var str3 = { 100: 'a', 2: 'b', 7: 'c' };
((str3));     //['2', '7', '100']
//The string returns the index(("string"));     //['0', '1', '2', '3', '4', '5']

()

Create a new array by calling the result of the function for each array element.

var str1 = [1,2,3,4,5,6]
const a = (item => item * 10)
(a);     // [10, 20, 30, 40, 50, 60]

()

Delete the last element of the array and return that element. The method changes the length of the array.

var str1 = [1,2,3,4,5,6]
((), str1);     // 6  ,  [1, 2, 3, 4, 5]

()

Adds a new element to the end of the array and returns the new length. The method changes the length of the array.

var str1 = [1,2,3,4,5,6]
((7), str1);     // 7 , [1, 2, 3, 4, 5, 6, 7]

() and reduceRight()

Reduce() execution order is from left to right. ReduceRight() execution order is from right to left.

The callback function is executed in turn for each element in the array, which is generally used in the accumulator.

There are four parameters, the first one is the initial value, or the return value after the calculation is completed. The second is the current element. The third is the index of the current element. The fourth is the array object to which the current element belongs.

Initiiavalue: When the array is empty, the initial value passed to the function can be set and placed behind the object

var str1 = [1,2,3,4,5,6]
// Use of accumulator((function(total,num,index,add){return total+num}));// 21
//Use of initiiavaluevar arr=[]
((function(total){return total},0));    //0

()

Reverses the order of elements in the array.

var str1 = [1,2,3,4,5,6]
(());     // [6, 5, 4, 3, 2, 1]

()

Delete the first element of the array and return that element.

var str1 = [1,2,3,4,5,6]
((),str1);     // 1 , [2, 3, 4, 5, 6]

()

Select part of the array and return the new array.

var str1 = [1,2,3,4,5,6]
// Two parameters start from index 1 to end from index 3, excluding the end position, forming a new array((1,3));     //  [2, 3]
// A parameter indicates that all elements starting with index 1 form a new array((1));     //  [2, 3, 4, 5, 6]

()

Checks whether any elements in the array pass the test. It's similar to every()

Some will not be executed if it encounters true. If all are false, return false. The original array will not be changed

var str1 = [1,2,3,4,5,6]
function some(item,index,array){
return item>5
}
((some));  //true
function some(item,index,array){
return item>7
}
((some));  //false

()

If srot() does not take parameters, it will sort the elements in the array alphabetically, that is, sort them in the order of character encoding.

If it is a number type, it is not actually sorted from small to large normally, but it is more like sorting according to the size of the first digit of the element.

If you want to sort from large to small, from small to large normally. The size of the value can be compared to achieve it.

From small to large, the meaning of a-b: a-b is greater than 0, which means a is large, so put a behind. a-b is less than 0, which means b is large, and b is at the end. If a-b is equal to 0, it remains unchanged. From large to small, it is the opposite of the above, so I won’t say it (I don’t want to write it anymore, the virtue of programmers is laziness)

As for why it is a - b, b - a, you can read this articlehttps:///article/

var arr1 = ['a', 'd', 'c', 'b'];
();  //['a', 'b', 'c', 'd']
var arr2 = [10, 5, 40, 25, 100, 1];
(); //[1, 10 ,100, 25, 40, 5]
//Sorting from small to largevar arr2 = [10, 5, 40, 25, 100, 1];
((a,b) => a-b)
(arr2);    // [1, 5, 10, 25, 40, 100]
//Sorting from large to smallvar arr2 = [10, 5, 40, 25, 100, 1];
((a,b) => b-a)
(arr2);    // [100, 40, 25, 10, 5, 1]

()

Add/remove elements from the array.

var str1 = [1,2,3,4,5,6]
// Delete, start with index of 1, delete 2((1,2), str1);     // [2, 3] , [1, 4, 5, 6]
// Add, replace with '0' from index 1 to index 2((1,2,'0','0'), str1);     // [2, 3] , [1, '0', '0', 4, 5, 6]

()

Converts the array to a string and returns the result.

var str1 = [1,2,3,4,5,6]
((), str1);     // 1,2,3,4,5,6  [1, 2, 3, 4, 5, 6]

Adds a new element to the beginning of the array and returns the new array length value.

var str1 = [1,2,3,4,5,6]
((0), str1);     // 7  , [0, 1, 2, 3, 4, 5, 6]

()

The valueOf() method returns itself.

var str1 = [1,2,3,4,5,6]
(7)
((),str1);     //  [1, 2, 3, 4, 5, 6, 7] , [1, 2, 3, 4, 5, 6, 7]

Summarize

That’s all for this article. I hope it can help you, and I hope you can pay more attention to more of my content!