Example shows how to manipulate Array in JS

  • 2020-03-30 02:56:28
  • OfStack

Js array elements add and delete has been more confused, today finally found the detailed information, first give me a 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 : 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,...
 When you empty the array, you just pass it startIndex . 
 If you do not delete all elements, then pass deleteCount Parameters. 
splice Also has the function of adding after deleting first, that is to delete a few elements, and then in the deleted location to add a number of elements, delete and add the number of elements must not be equal, at this time hou deleteCount That's what we're going to use. 
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);
}


Related articles: