Talking about the Addition and Deletion of Arrays in JavaScript

  • 2021-06-29 06:19:57
  • OfStack

Increase of array

•ary.push()

Add an element to the end of the array, returning the length of the new array after it was added, changing the original array

•ary.unshift()

Add an element to the beginning of the array, returning the length of the new array after it was added, changing the original array

• var ary=[1,2,3,4];
var res=ary.unshift(6);
console.log(res); ---- > 5

Returns the length of the new array &8226;ary.splice (n, m, x) deletes m elements from index n, puts the new element X in front of index n, returns the deleted elements as a new array, and changes the original array.

•ary.splice(n,0,x)

Starting with index n, delete 0 elements, place the newly added element x before index n, and return an empty array with the original array changed.

•ary.splice(n,m)

Remove m elements from index n, return the deleted content as a new array, change the original array

•splice(0,0,x)----- > unshift

Delete of Array

&8226;ary.pop() deletes the last item of the array, returns the deleted item, the original array changes

&8226;ary.shift() deletes the first item of the array, returns the deleted item, the original array changes

•var ary=[5,8,3,4,6];var res=ary.shift();console.dir(res);---- > 5 Returns the first item of the array &8226;Delete the contents of the last item of the array ary.splice (ary.length-

1,1) //ary.length-1 Array last item content ary.length-=1 ary.length--

•var ary=[5,8,3,4,6];//

ary.splice(ary.length-1,1);//
ary.length-=1;
console.dir(ary);---- >

Output is query and copy of [5,8,3,4] array

&8226;slice (n, m) starts from index n, finds index m, returns the found content as a new array, the original array does not change

&8226;slice (n-1, m) extracts items n to m from the array

&8226;slice (n) finds the end of the array starting with index n

&8226;slice(0) slice() duplicates an original array into an array clone

&8226;concat() can also clone arrays

&8226;The intent of concat is to stitch two arrays together, ary.concat (ary2)

Array to String

&8226;tostring takes out each item of the array separated by commas, leaving the original array unchanged
&8226;join ('+') takes out each item of the array separated by a specified delimiter

&8226;Sum of Arrays


var ary=[5,8,3,4,6];
var str=ary.join("+");
var total=eval(str);
console.dir(total); // Turns the specified string into a true expression execution 



 var ary=[5,8,3,4,6];
   var total=0;
   for(var i=0;i<ary.length;i++){
     total+=ary[i];
   }
   console.dir(total); 

Array arrangement and ordering

&8226;reverse() sorts the array upside down, changing the original array

&8226;sort can sort from large to small or from small to large, but direct writing sort can only sort numbers ary.sort (function (a, b){return (a-b)) within 10;

1 Some commonly used methods are incompatible

&8226;The indexOf() method returns the position of the first occurrence of a specified string value in a string.
•foreach
•map

In the future, I will continue to add hope to help you understand learning together.


Related articles: