SoFunction
Updated on 2025-02-28

Detailed explanation of JS array method

1. Will modify the original array

():

(At the end of the array) Add a new element to the array

push() method returns the length of the new array

var fruits = ["Banana", "Orange", "Apple", "Mango"];
("Kiwi");  

():

Method removes the last element from the array

You can receive the return value of pop(), which is the pop-up value "Mango"

var fruits = ["Banana", "Orange", "Apple", "Mango"];
("Kiwi");  

():

Delete the first array element

Can receive deleted values

var fruits = ["Banana", "Orange", "Apple", "Mango"];
();

():

(At the beginning) Add new elements to the array

Returns the length of the new array.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
("Lemon");

():

Used to add new items to an array

The first parameter (2) defines the location (stitching) where new elements should be added.

The second parameter (0) defines how many elements should be deleted.

The remaining parameters ("Lemon", "Kiwi") define the new element to be added.

The splice() method returns an array containing deleted items.

You can also delete elements in the array by setting parameters

var fruits = ["Banana", "Orange", "Apple", "Mango"];
(2, 0, "Lemon", "Kiwi");
//["Banana","Orange","Lemon","Kiwi","Apple","Mango"]
 var fruits = ["Banana", "Orange", "Apple", "Mango"];
(0, 1);
//["Orange", "Apple", "Mango"]

():

Sort arrays in alphabetical order

If you sort numbers, you need to pay attention. "25" is greater than "100" because "2" is greater than "1". We correct this problem with a ratio function.

sort() can also sort object arrays by modifying the comparison function.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
(); 
 var points = [40, 100, 1, 5, 25, 10];
(function(a, b){return a - b});//Ascending order(function(a, b){return b - a});//Dimmersive order((a, b)=>{return b - a});//Arrow function var cars = [
    {type:"Volvo", year:2016},
    {type:"Saab", year:2001},
    {type:"BMW", year:2010}
]
(function(a, b){return  - });//Compare year (digit)(function(a, b){//Compare Types (String)	  var x = ();
	  var y = ();
	  if (x < y) {return -1;}
	  if (x > y) {return 1;}
	  return 0;
});

():

Invert elements in an array

var fruits = ["Banana", "Orange", "Apple", "Mango"];
();  

2. Do not modify the original array

():

Convert an array into a string with array values ​​(comma-separated).

var fruits = ["Banana", "Orange", "Apple", "Mango"]
(())
//Banana,Orange,Apple,Mango

():

All array elements can be combined into one string.

It behaves like toString(), but it can also specify a separator

var fruits = ["Banana", "Orange", "Apple", "Mango"]
((" * "))
//Banana * Orange * Apple * Mango

():

Create a new array by combining (concatenating) existing arrays. Multiple connections can be made

var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias", "Linus"];
var myChildren = (myBoys);   // Connect myGirls and myBoys var arr1 = ["Cecilie", "Lone"];
var arr2 = ["Emil", "Tobias", "Linus"];
var arr3 = ["Robin", "Morgan"];
var myChildren = (arr2, arr3);   // Willarr1、arr2 and arr3 Connected together

() :

Method uses a fragment of the array to cut out a new array.

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = (1);//From the first to the last//["Orange", "Lemon", "Apple", "Mango"]
 var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = (1,3);//From the first to the third (not including 3)//["Orange", "Lemon"]

():

Call a provided function for each element in the array, and the result is returned as a new array without changing the original array

let arr = [1, 2, 3, 4, 5]
let newArr = (x => x*2)//Abbreviated arrow function//arr= [1, 2, 3, 4, 5] The original array remains unchanged//newArr = [2, 4, 6, 8, 10] Return a new array

():

Execute each element in the array with the provided function without return value, please note that it is different from the map method

let arr = [1, 2, 3, 4, 5]
(x => {
    (2*x)
    //Return x*2 The return value is useless, this function does not return the value})

():

This method is to judge all elements and return elements that meet the conditions as a new array. The function says the condition! ! !

let arr = [1, 2, 3, 4, 5]
let newArr = (value => value >= 3 )
//orlet newArr = (function(value) {return value >= 3} )
(newArr)
//[3,4,5]

():

This method returns a Boolean value by judging all elements. If all elements meet the judgment conditions, it returns true, otherwise it is false

let arr = [1, 2, 3, 4, 5]
const isLessThan4 = value => value < 4
const isLessThan6 => value => value < 6
(isLessThan4 ) //false
(isLessThan6 ) //true

():

This method returns a Boolean value by judging all elements. If there is an element that meets the judgment condition, it returns true. If all elements do not meet the judgment condition, it returns false

let arr= [1, 2, 3, 4, 5]
const isLessThan4 = value => value < 4
const isLessThan6 = value => value > 6
(isLessThan4 ) //true
(isLessThan6 ) //false

():

This method is when all elements call the return function, the return value is the last result, and the value passed in must be of the function type

let arr = [1, 2, 3, 4, 5]
const add = (a, b) =&gt; a + b
let sum = (add)  
 (sum) //sum = 15 is equivalent to the accumulated effect//There is another corresponding one () method,The difference is that this is operated from right to left

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!