This example summarizes the common operation methods of js arrays. Share it for your reference, as follows:
var arr = [1, 2, 3, 4, 5];//Delete and return the first element in the array var theFirst = arr.shift();alert(theFirst);//Return 1 numberert(arr);//2,3,4,5 object//Delete and return the last element in the array var theLast = arr.pop();alert(theLast);//Return 5 numberert(arr);//2,3,4 object//Add one or more elements at the beginning of the array and return the array length var theNewArrStart = arr.unshift(-1, 0);alert(theNewArrStart);//Return 5 numberlert(arr);//-1,0,2,3,4 object//Add one or more elements at the end of the array and return the array length var theNewArrEnd = arr.push(5, 6);alert(theNewArrEnd);//Return 7 numberlert(arr);//-1,0,2,3,4,5,6 object//Delete n elements from the i-th (array index) position arr.splice(1, 2);alert(arr);//-1,3,4,5,6 object//Delete n elements from the i-th (array index) position and insert s new elements arr.splice(1, 2, 10, 11, 12);alert(arr);//-1,10,11,12,5,6 object//Merge 2 or more arrays (the parameters in concat can be a single value or an array, and there can be multiple values or arrays) var arr1 = [7, 8];var arrCon = arr.concat(arr1);alert(arrCon);//-1,10,11,12,5,6,7,8 object//Separate elements in the array with specific characters and return a string (defaults to commas if no specific split character is set) var theSep = arrCon.join('-');alert(theSep);//-1-10-11-12-5-6-7-8 string//The order of points to elements in the array var theRev = arrCon.reverse();alert(theRev);//8,7,6,5,12,11,10,-1For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript array operation techniques", "Summary of JavaScript mathematical operation usage methods", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques" and "Summary of JavaScript traversal algorithms and techniques"
I hope this article will be helpful to everyone's JavaScript programming.