当前位置:网站首页>JS foundation - array object

JS foundation - array object

2022-06-11 09:38:00 Snatch bamboo shoots 123

Array object

Array creation

Create format

var  Array name =new Array( Elements 1, Elements 2,... , Elements n);
var  Array name =[ Elements 1, Elements 2,... , Elements n];

The assignment of an array

// Create an array 
var arr=[];
//  Assignment by subscript 
arr[i]= value ;

Array length acquisition

// Create an array 
var arr=[];
//length Is an attribute of an array object 
arr.length;  //0

Intercept array elements

arr.slice(start,end);

Similar to slicing , The interception range is [start,end), And the subscript of the first element of the array is 0

Array elements are added

Add a new element to the beginning of the array ( Change the original array )

arr.unshift( The new element 1, The new element 2,..., The new element n);

Add a new element to the end of the array ( Change the original array )

arr.push( The new element 1, The new element 2,..., The new element n);
// or 
arr[n]= value 1;
arr[n+1]= value 2;

Delete array elements

Delete the last element of the array ( Change the original array )

arr.pop()

Delete and return the first element in the array ( Change the original array )

arr.shift();

The array is upside down

Reverse arrangement of all elements

arr.reverse();

Array sorting

 Array name .sort();

Conversion of array and string

Join all the elements in the array into a string ( Do not change the original array )

 Array name .join(" Connector ");
 String name .split(" Connector ");

Example

arr=[" Qin Shi "," bright moon "," han "," when "," Turn off "]
str1=arr.join("**");   //  Qin Shi ** bright moon ** han ** when ** Turn off 
arr1=str1.split("**");   // [" Qin Shi "," bright moon "," han "," when "," Turn off "]
原网站

版权声明
本文为[Snatch bamboo shoots 123]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110914078519.html