Array method summary in javascript

  • 2020-07-21 06:51:44
  • OfStack

js array elements add and delete 1 has been more confusing, today finally found the detailed information, first give me the test code ^-^


var arr = new Array();
arr[0] = "aaa";
arr[1] = "bbb";
arr[2] = "ccc";
//alert(arr.length);//3
arr.pop();
//alert(arr.length);//2
//alert(arr[arr.length-1]);//bbb
arr.pop();
//alert(arr[arr.length-1]);//aaa
//alert(arr.length);//1
 
var arr2 = new Array();
//alert(arr2.length);//0
arr2[0] = "aaa";
arr2[1] = "bbb";
//alert(arr2.length);//2
arr2.pop();
//alert(arr2.length);//1
arr2 = arr2.slice(0,arr2.length-1);
//alert(arr2.length);//0
arr2[0] = "aaa";
arr2[1] = "bbb";
arr2[2] = "ccc";
arr2 = arr2.slice(0,1);
alert(arr2.length);//1
alert(arr2[0]);//aaa
alert(arr2[1]);//undefined

shift: Removes item 1 of the original array and returns the value of the deleted element; Returns undefined if the array is empty


var a = [1,2,3,4,5];
var b = a.shift(); //a : [2,3,4,5]  b : 1
 

unshift: Adds the argument to the beginning of the original array and returns the length of the array


var a = [1,2,3,4,5];
var b = a.unshift(-2,-1); //a : [-2,-1,1,2,3,4,5]  b : 7

Note: The return value of the test under IE6.0 is always undefined, and the return value of the test under FF2.0 is 7. Therefore, the return value of this method is not reliable, so splice can be used instead of this method when the return value is needed.

pop: Deletes the last item of the original array and returns the value of the deleted element; Returns undefined if the array is empty


var a = [1,2,3,4,5];
var b = a.pop(); //a : [1,2,3,4]  b : 5 // You don't have to return it, you just call it 
 

push: Adds the argument to the end of the original array and returns the length of the array


var a = [1,2,3,4,5];
var b = a.push(6,7); //a : [1,2,3,4,5,6,7]  b : 7
 

concat: Returns 1 new array that is formed by adding parameters to the original array


var a = [1,2,3,4,5];
var b = a.concat(6,7); //a : [1,2,3,4,5]  b : [1,2,3,4,5,6,7]
 

splice (start deleteCount, val1 val2,...). : Delete the deleteCount entry from the start location and insert val1,val2,...


var a = [1,2,3,4,5];
var b = a.splice(2,2,7,8,9); //a : [1,2,7,8,9,5]  b : [3,4]
var b = a.splice(0,1); // with shift
a.splice(0,0,-2,-1); var b = a.length; // with unshift
var b = a.splice(a.length-1,1); // with pop
a.splice(a.length,0,6,7); var b = a.length; // with push
 

reverse: Reverse order the array


var a = [1,2,3,4,5];
var b = a.reverse(); //a : [5,4,3,2,1]  b : [5,4,3,2,1]
 

sort(orderfunction) : Sorts the array by the specified parameter


var a = [1,2,3,4,5];
var b = a.sort(); //a : [1,2,3,4,5]  b : [1,2,3,4,5]
 

slice(start,end) : Returns a new array of items from the original array that specify the starting and ending subscripts


var a = [1,2,3,4,5];
var b = a.slice(2,5); //a : [1,2,3,4,5]  b : [3,4,5]
 

join(separator) : Sets the elements of an array into 1 string, delimited by separator, and omitted by default comma


var a = [1,2,3,4,5];
var b = a.shift(); //a : [2,3,4,5]  b : 1
 
0

Here's another way to simulate javaStringBuffer processing strings using arrays:


var a = [1,2,3,4,5];
var b = a.shift(); //a : [2,3,4,5]  b : 1
 
1

Today, in my application, I suddenly found that join is a good way to convert an array into a string. Therefore, it is used to encapsulate an object:


var a = [1,2,3,4,5];
var b = a.shift(); //a : [2,3,4,5]  b : 1
 
2

This is the end of this article, I hope you enjoy it.


Related articles: