On the _javascript of adding and deleting the array in JavaScript

Source: Internet
Author: User

An increase in the array

Ary.push ()

Adds an element to the end of the array, returns the length of the new array added, and the original array changes

Ary.unshift ()

Adds an element to the beginning of the array, returns the length of the new array added, and the original array changes

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

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

Ary.splice (n,0,x)

Deletes 0 elements from index N, puts the newly added element x before the index n, returns an empty array, and changes the original array.

Ary.splice (n,m)

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

Splice (0,0,x)----->unshift

The deletion of an array

Ary.pop () deletes the last item of the array, returns the deleted item, and the original array changes

Ary.shift () deletes the first item of the array, returns the deleted item, and 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 • Deletes the contents of the last item in the array Ary.splice (ary.length-

1,1)//ary.length-1 the contents 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);---->

Query and copy of [5,8,3,4] array output

Slice (n,m) starts at index N, finds the index m, returns the found content as a new array, and does not change the original array

Slice (n-1,m) extracts the nth item of an array to M

Slice (n) finds the end of the array starting at index n

Slice (0) slice () copy an existing array into an array clone

concat () can also implement array cloning

Concat's intention is to implement the concatenation of arrays Ary.concat (Ary2) to stitch two arrays

Array into a string

tostring Each item of the array, separated by commas, and the original array is unchanged.
Join ("+") takes each item of the array and separates it with the specified delimiter

• Array summation

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



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

arrangement and sorting of arrays

reverse () sort the array upside down, and the original array changes

sort can be done from large to small or from small to large, but direct write sort can only be sorted within 10 of the number Ary.sort (function (a,b) {return (a-b);})

Some common methods, but not compatible.

The indexof () method returns the location of the first occurrence of a specified string value in a string.
foreach
Map

will be in the future of the virtual seats continue to complement the hope that can help you understand learning together.

Above this article on the JavaScript in the array of additions and deletions is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.