Javascript appends methods to arrays

  • 2020-03-30 03:20:13
  • OfStack

Javascript to add elements to an array is a very simple problem,javascript itself provides a large number of such functions, we can use js functions to quickly add elements to an array, this article on javascript to make the method of array append as follows.

In the past, arr.concat(arr2) was commonly used to append arrays. An example is as follows:


<script type="text/javascript">
var arr = new Array(3);
arr[0] = " The test array 1 for  1";
arr[1] = " The test array 1 for  2";
arr[2] = " The test array 1 for  3";
var arr2 = new Array(3);
arr2[0] = " The test array 2 for  1";
arr2[1] = " The test array 2 for  2";
arr2[2] = " The test array 2 for  3";
document.write(arr.concat(arr2)+"<br/>");
document.write(arr.concat(" You big ye 2"," You big ye 3")+"<br/>")
</script>

Many people have been confused about the addition and deletion of js array elements, and the following test code is given:

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 : deletes the first item of the original array and returns the value of the deleted element; Returns if the array is empty undefined 
var a = [1,2,3,4,5]; 
var b = a.shift(); //a : [2,3,4,5]   b : 1
unshift : adds an 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: in IE6.0 The test return value is always undefined . FF2.0 The test return value is 7 , so the return value of this method is not reliable and is available when the return value is needed splice Instead of this method. 
pop : deletes the last item of the original array and returns the value of the deleted element; Returns if the array is empty undefined 
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 and call it directly 
push : adds an 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 a new array that adds arguments 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,...) From: start Position start delete deleteCount Item and insert from that position 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 the shift
a.splice(0,0,-2,-1); var b = a.length; //With the unshift
var b = a.splice(a.length-1,1); //With the pop
a.splice(a.length,0,6,7); var b = a.length; //With a 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 parameters  
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 subscript to the ending subscript  
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) : groups the elements of an array into a string separator Is the delimiter, and the default is a comma for the delimiter  
var a = [1,2,3,4,5]; 
var b = a.join("|"); //a : [1,2,3,4,5]   b : "1|2|3|4|5"
 Let's do another simulation using arrays javaStringBuffer String handling method: 

function StringBuffer()
{
var arr = new Array;
this.append = function(str)
{
    arr[arr.length] = str; 
};
this.toString = function()
{
    return arr.join(""); //Ping the array append comes in as a string
};
}
 I found out all of a sudden in the application today join Is a good way to convert an array into a string, so encapsulating an object USES: 

function arrayToString(arr,separator)
{
if(!separator) separator = "";//Separator for null defaults to empty
    return arr.join(separator); 
}

function arrayFindString(arr,string)
{
var str = arr.join(""); 
    return str.indexOf(string); 
}


Finally, there is one about push.apply to append array when encountering some problems to like playing a.ush. Apply (a, b); This practice friend a test
Amy polumbo ush. Apply (a, b); It's a cool way to write without writing a for loop, and it hasn't had any problems until today when I wanted append's b to be a big array.


a = new Array();     
b = new Array(125624);                     
a.push.apply(a, b);


The above code throws the following exception under MAC chrome

Uncaught RangeError: Maximum call stack size exceeded

If I change the Array to b = new Array(125623); A smaller element is fine. Test the problem that every other browser has a large array to go wrong, but the threshold varies from browser to browser

The advice here is to use honest forEach, not only to avoid large array exceptions, but also to be the fastest from a performance perspective


Related articles: