Home > Web Front-end > JS Tutorial > body text

What are the commonly used methods for arrays?

清浅
Release: 2020-09-08 11:18:39
Original
79819 people have browsed it

Commonly used methods in arrays are: 1. The push method to add new content to the end of the array; 2. The pop method to delete the last item of the array; 3. The shift method to delete the first item of the array; 4. Add new content to the first part of the array using the unshift method and so on.

What are the commonly used methods for arrays?

[Recommended course: JavaScript Tutorial]

Some commonly used arrays Method:

1. push()

Add new content to the end of the array

Parameters: The item to be added. Pass multiple numbers separated by commas. Any data type is acceptable.

Return value: The length of the new array.

Whether to change the original array: Change

let ary1 = [12,34,26];
ary1.push(100); //返回一个新的长度 
length=4console.log(ary1)//结果为 [12,34,26,100]
Copy after login

2. pop()

Delete the last item of the array

Parameters: None

Return value: Whether the deleted item

has changed Original array: change

let ary2 = [108,112,39,10];
ary2.pop();//删除的最后一项为10
console.log(ary2);//[108, 112, 39]
Copy after login

3, shift()

Delete the first item of the array

Parameters: None

Return Value: deleted item

Whether to change the original array: change

let ary3 = [0,108,112,39];
ary3.shift();//删除的第一项为0
console.log(ary3);//[108, 112, 39]
Copy after login

4. unshift()

Add new content to the first part of the array

Parameters: items to be added, multiple items separated by ','

Return value: length of new array

Whether to change the original array: change

let ary4 = ['c','d'];
ary4.unshift('a','b');
console.log(ary4);//["a", "b", "c", "d"]
Copy after login

5. slice()

Find part of the content according to the conditions

Parameters:

array.slice(n, m), from Search from index n to m (excluding m)

array.slice(n) If the second parameter is omitted, search until the end

array.slice(0) and output the content as is , array cloning can be realized

array.slice(-n,-m) slice supports negative parameters, counting from the last item, -1 is the last item, and -2 is the penultimate item

Return value: Return a new array

Whether to change the original array: Do not change

let ary5 = [1,2,3,4,5,6,7,8,9]; 
//console.log(ary5.slice(2,8));//从索引2开始查找到索引为8的内容,结果为[3, 4, 5, 6, 7, 8] 
//console.log(ary5.slice(0)); 
console.log(ary5.slice(-2,-1));//[8]
Copy after login

6, splice()

to the array Add, delete, modify

Add: ary.splice(n,0,m) deletes 0 items starting from index n, and inserts m or more content in front of index n

Returns empty Array

Modification: ary.splice(n,x,m) deletes x items starting from index n, m replaces the deleted part

Delete the original content, and then replace it with the new content

Delete: ary.splice(n,m) Delete m contents starting from index n

(If the second parameter is omitted, delete from n to the end)

Return the deleted new array, the original array changes

//增加
  let ary6_z = [33,44,55,66,77,88];
  ary6_z.splice(2,0,'a','b')
  console.log(ary6_z);// [33, 44, "a", "b", 55, 66, 77, 88]

  //修改
  let ary6_x = [33,44,55,66,77,88];
  ary6_x.splice(1,2,'x','y')
  console.log(ary6_x);// [33, "x", "y", 66, 77, 88]

  //删除
   let ary6_s = [33,44,55,66,77,88];
   //console.log(ary6.splice(3,2))//[66, 77]
   console.log(ary6_s.splice(3));//[66, 77, 88]
Copy after login

7, join()

Use the specified delimiter to splice each item of the array into a string

Parameter: Specified delimiter (if this parameter is omitted, comma is used as the delimiter)

Return value: The spliced ​​string

Whether the original array is changed: Do not change

let ary7 = [1,2,3];
console.log(ary7.join('、'));//1、2、3
Copy after login

8, concat()

Used to connect two or more arrays

Parameters: Parameters can be specific values, It can also be an array object. It can be any number of

Return value: Return the new array after connection

Whether to change the original array: No change

let ary8 = ['你'];
let ary80 = ary8.concat('好');
console.log(ary80);//["你", "好"]
Copy after login

9, indexOf()

Detect the index of the first occurrence of the current value in the array

Parameters: array.indexOf(item,start) item: the element being searched for start: the position in the string where the search begins

Return value: The index found for the first time, if not found, return -1

Whether to change the original array: No change

let ary9 = ['a','b','c','d','e','a','f'];   
console.log(ary9.indexOf('c'));//2
console.log(ary9.indexOf('a',3))//5
Copy after login

10, lastIndexOf()

Detect the position index of the last occurrence of the current value in the array

Parameters: array.lastIndexOf(item,start) item: the element to be searched for start: the start of the search in the string Position

Return value: The index found for the first time, if not found, return -1

Whether to change the original array: No change

let ary10 = ['a','b','c','d','e','a','f'];   
console.log(ary10.lastIndexOf('c'));//2
console.log(ary10.lastIndexOf('f',1))//-1
Copy after login

11, includes( )

Judge whether an array contains a specified value

Parameter: Specified content

Return value: Boolean value

Whether the original value is changed Array: Do not change

let ary13 = ['a','b','c','d']; 
console.log(ary13.includes('c'));//true
console.log(ary13.includes(2));//false
Copy after login

12, sort()

Sort the elements of the array (the default is to sort from small to large and based on strings) )

Parameter: Optional (function) Specifies the sorting rule and the default sort order is in ascending alphabetical order

Return value: New array after sorting

Whether to change the original array: Change

sort can only process numbers within 10 (single digits) without passing parameters

 let ary11 = [32,44,23,54,90,12,9]; 
   ary11.sort(function(a,b){        // return a-b;  // 结果[9, 12, 23, 32, 44, 54, 90]
        // return b-a;  // 结果[90, 54, 44, 32, 23, 12, 9]   })  
    console.log(ary11);
Copy after login

13, reverse()

Array the array upside down

Parameters: None

Return value: New array after reverse order

Whether to change the original array: Change

let ary12 = [6,8,10,12]; 
console.log(ary12.reverse());//[12, 10, 8, 6]
Copy after login

14, forEach()

Loop through each item of the array

Parameters: function ary.forEach(function(item,index,ary){}) item: each item index: index ary: Current array

Return value: None

Whether to change the original array: No change

continue and break cannot be used in forEach, and cannot jump out in forEach, only skip (return skip)

 let ary14 = ['a','b','c','d']; 
 let item = ary14.forEach(function(item,index,ary){
        console.log(item,index,ary);
   })
Copy after login

The above is the detailed content of What are the commonly used methods for arrays?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template