Summary of Common Operation Methods of js Array of Add Delete Merge Split etc

  • 2021-07-07 06:18:12
  • OfStack

This paper summarizes the common operation methods of js array with examples. Share it for your reference, as follows:


var arr = [1, 2, 3, 4, 5];
// Deletes and returns the 1 Elements 
var theFirst = arr.shift();
alert(theFirst);// Return 1 number
alert(arr);//2,3,4,5 object
// Delete and return the last 1 Elements 
var theLast = arr.pop();
alert(theLast);// Return 5 number
alert(arr);//2,3,4 object
// Add at the beginning of the array 1 Elements or elements, and returns the length of the array 
var theNewArrStart = arr.unshift(-1, 0);
alert(theNewArrStart);// Return 5 number
alert(arr);//-1,0,2,3,4 object
// Add at the end of the array 1 Elements or elements, and returns the length of the array 
var theNewArrEnd = arr.push(5, 6);
alert(theNewArrEnd);// Return 7 number
alert(arr);//-1,0,2,3,4,5,6 object
// From the first i (Array index) Position deletion n Elements 
arr.splice(1, 2);
alert(arr);//-1,3,4,5,6 object
// From the first i (Array index) Position deletion n Elements, and insert s New elements 
arr.splice(1, 2, 10, 11, 12);
alert(arr);//-1,10,11,12,5,6 object
// Merge 2 One or more arrays ( concat The parameters in can be single values or arrays, and can have multiple values or arrays) 
var arr1 = [7, 8];
var arrCon = arr.concat(arr1);
alert(arrCon);//-1,10,11,12,5,6,7,8 object
// Separates elements in an array with specific characters and returns a string (defaults to commas if no specific dividing character is set) 
var theSep = arrCon.join('-');
alert(theSep);//-1-10-11-12-5-6-7-8 string
// Order of points to elements in an array 
var theRev = arrCon.reverse();
alert(theRev);//8,7,6,5,12,11,10,-1

More readers interested in JavaScript can check out the topics of this site: "Summary of JavaScript Array Operation Skills", "Summary of JavaScript Mathematical Operation Usage", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Effects and Skills", "Summary of JavaScript Error and Debugging Skills" and "Summary of JavaScript Traversal Algorithms and Skills"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: