Addition of array
•ary.push()
Add an element to the end of the array, which returns the length of the new array after being added, and the original array is changed
•ary.unshift()
Adding elements to the beginning of the array returns the length of the new array after adding, and the original array is changed
• var ary=[1,2,3,4];
var res=ary.unshift(6);
console.log(res); --->5
What is returned is the length of the new array •ary.splice(n,m,x) to delete m elements from index n, place the newly added element X in front of index n, and return the deleted element as a new array, and change the original array.
•ary.splice(n,0,x)
0 elements are deleted from index n, and the newly added element x is placed in front of index n. The returned empty array is changed.
•ary.splice(n,m)
Start deleting m elements from index n, return the deleted content as a new array, and change the original array
•splice(0,0,x)----->unshift
Deletion of arrays
•ary.pop() deletes the last item of the array, returns the deleted item, and the original array is changed
•ary.shift() deletes the first item of the array, returns the deleted item, and the original array is changed
•var ary=[5,8,3,4,6];var res=ary.shift();console.dir(res);---->5 Return the first item of the array • Delete the content of the last item of the array ary.splice(ary.length-
1,1) //ary.length-1 The content of the last item of the array ary.length-=1 ary.length--
•var ary=[5,8,3,4,6];//
ary.splice(ary.length-1,1);//
ary.length-=1;
console.dir(ary);---->
The output is query and copying of [5,8,3,4] array
•slice(n,m) starts from index n, finds index m, and returns the found content as a new array, and the original array does not change.
•slice(n-1,m) extracts the nth term to mth term of the array
•slice(n) Find the end of the array starting from index n
•slice(0) slice() Copy the original array into an array clone
•concat() can also implement array cloning
•The original intention of concat is to implement array stitching ary.concat(ary2) to splice two arrays
Convert arrays into strings
•Tostring takes out each item in the array and separates it with a comma, and the original array remains unchanged
•join("+") separates each item in the array with a specified separator
•Sum of arrays
var ary=[5,8,3,4,6];var str=ary.join("+");var total=eval(str);console.dir(total); //Change the specified string into a real expression and execute 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 sorting
•reverse() Sort the array inverted and change the original array
•sort can realize sorting from large to small or from small to large, but writing sort directly can only sort numbers within ten.sort(function(a,b){return (ab);})
Some commonly used methods are incompatible
•indexOf() method returns the location where a specified string value first appears in the string.
•foreach
•map
I will continue to supplement in the future in the crowded room. I hope it can help everyone understand and learn together.
The above brief discussion on the addition, deletion, modification and search of arrays in JavaScript is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.