How to manipulate arrays in JavaScript

Source: Internet
Author: User

The array in JS is also the object data type, also consists of the property name and the property value, the property name of the array is the corresponding index value of each item in the array, the property value of the array is the value of each item written in our array.

Some methods in the array

1. Start position increment and delete

Push: Adds a new element to the end of the array, returns the length of the added array, and changes the original array .

Pop: Deletes the element at the end of the array, returns the deleted element, and changes the original array .

Unshift: Adds an element to the head of the array, returns the length of the added array, and changes the original array .

Shift: Delete the first element of the array, return the deleted content, change the original array

2, concat first function when the parameter is empty, the cloned array returns an array that is the same as the original array, and the original array does not change .

The second function splicing two arrays, returning an array after stitching, and the original array does not change .

3, Slice (n,m) starting from index n (inclusive), find the index m (not included), will find the content returned a new array, the original array does not change .

Slice () and slice (0) clone a new array, and the function is null with the concat parameter.

Slice (n) starts at index n (contains), finds the end of the array, returns a new array with the found content, and does not change the original array .

4. How to transform an array into a string

Join (character): Splits the array into strings according to the specified characters, and the original array does not change.

Join () does not write a string, the default is separated by commas, this is equivalent to another method tostring

ToString converts an array into a string, which is separated by default, and the original array does not change.

5, Splice

    • Splice (n,m) starts with index n (containing n), deletes the number of M, returns the deleted element to a new array, and changes the original array .
    • Splice (n) starts with index n (containing n) and deletes all subsequent elements, and if n is 0, it is the clone array, which changes the original .
    • Splice (n,m,x) starts with index n (containing n), deletes m elements, places x in the deleted position, implements substitution, returns the deleted element, and changes the original array
    • Splice (N,0,X) adds x to the array from index n to change the original array .

6. Sorting and arranging

Reverse: The elements in the original array are arranged upside down and the original array is changed.

Sort: The original array is sorted from small to large, but this method cannot handle more than 10 of the numbers, because the default sort method is sorted by the Asii code value of the elements in the array.

         var ary=[12,34,123,2,67,78];         Ary.sort (A, b) {return// Small to large sort           ary.sort (function (A, b) {return// from large to small sort

7, IndexOf: In the array to find our index position of the specified element, return the position of the index, the original array is unchanged, with this method, we implement to determine whether the original array contains an element, if there is no return 1, there is the corresponding index

Not compatible in Ie6-8.

var ary=[15,8,88,3,85,1]; function indexOf (item,ary) {  index=-1;    for (var i=1,j=ary.length;i<j;i++) {       if(item===ary[i]) {          Index= i;            Break ;        }    }     return index;  } Console.log (IndexOf (15,ary));

  

How to manipulate arrays in JavaScript

Related Article

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.