JS array operation (array add, delete, flip, turn string, Fetch index, intercept (slice) slice, splice splice, array merge) _javascript tips

Source: Internet
Author: User

POP deletes last item

Deletes the last item and returns the value of the deleted element, or returns Undefine if the array is empty

var a = [1,2,3,4,5];
A.pop ();//a:[1, 2, 3, 4]
a.pop ();//a:[1, 2, 3]

Shift deletes the first item

Deletes the first item of the original array and returns the value of the deleted element, or returns Undefine if the array is empty

var a = [1,2,3,4,5]; 
A.shift (); a:[2,3,4,5]

Push increases to the end

and returns the length of the new array;

var a = [1,2,3,4,5]; 
A.push (6);//[1, 2, 3, 4, 5, 6]
aa.push (' xx ');//[1, 2, 3, 4, 5, 6, "XX"] return length 7

Unshift added to the front

and returns the length of the new array;

var a = [1,2,3,4,5]; 
A.unshift ();//[1, 2, 3, 4, 5]
a.unshift ("cc");//["CC", 1, 2, 3, 4, 5] return length 6

Reverse Array Flip

and returns the inverted original array, the original array flips the

var a = [1,2,3,4,5]; 

The join array is turned into a string

and returns the string, the original array of wood-changed

var a = [1,2,3,4,5]; 
var b=a.join (' | | | '); /b: "1| | 2| | 3| | 4| | 5 "a:[1,2,3,4,5] 

IndexOf array Element Index

and returns the index of the element, no return-1, and the index starts at 0

var a = [' A ', ' B ', ' C ', ' d ', ' e ']; 
A.indexof (' a ');//0
A.indexof (a);//-1
a.indexof (' f ');//-1
a.indexof (' e ');//4

Slice intercept (slice) array to get an intercepted array

Returns a new array of entries from the original array that specify the start index (contained) to the end index (not included), the original array is Woody, and the index starts at 0

var a = [' A ', ' B ', ' C ', ' d ', ' e ']; 
A.slice (1,3);//["B", "C"] a:[' a ', ' B ', ' C ', ' d ', ' e ']
a.slice (0,4);//["A", "B", "C", "D"]
a.slice (3,4);//["D"]

Splice splicing array The original array changes can be realized shift before the deletion, pop Delete, unshift before the increase, with the same push after the same effect

Returns the edited array of elements, the original array, and the index starting at 0.

 * * parameter is 2///The first parameter is the index (starting from 0), and the second is the length var a = [' A ', ' B ', ' C ', ' d ', ' e ']; 
A.splice (0,2);//["A", "B"] a:["C", "D", "E"] A.splice (0,2),//["C", "D"] a:["E"] var a = [' A ', ' B ', ' C ', ' d ', ' e ']; A.splice (0,1);//["a"] a:["B", "C", "D", "E"] with shift before deleting var a = [' A ', ' B ', ' C ', ' d ', ' E '] a.splice (a.length-1,1) l//["E"] a:["a "," B "," C "," D "] with pop before delete/* parameter greater than 2///splice (Start,deletecount,val1,val2,...) 
: Deletes the DeleteCount entry from the start position and inserts Val1,val2,... var a = [' A ', ' B ', ' C ', ' d ', ' e '] from that location; 
A.splice (3,1,10,21,238,99);//["D"] a:["a", "B", "C", "N", 238, "F", "E"] var a = [' A ', ' B ', ' C ', ' d ', ' e ']; A.splice (a.length,100000000,88)/return [] from the element behind the last element, intercept the length of any, it must be empty a:["a", "B", "C", "D", "E", 88] Add var a = [' A ', ' B ', '] with push 
C ', ' d ', ' e ']; A.splice (a.length,0,88)/return [] from the element behind the last element, intercept the length of any, certainly the empty a:["a", "B", "C", "D", "E", "88]" with the push after adding var a = [' A ', ' B ', ' C ', ' d ', ', '
E ']; A.splice (0,0,88,99)/return [] from the first element, intercept the length of 0 is definitely empty a:[88, the, "a", "B", "C", "D", "E"] with Unshift before adding 

Concat array Merge

Returns the merged new array, the original array of wood changes

var a = [' A ', ' B ', ' C ', ' d ', ' e ']; 
A.concat ([88,99]);//["A", "B", "C", "D", "E", "a:[", "a", "B", "C", "D", "E"]
var b= [9999,10000]
a.concat (b); ["A", "B", "C", "D", "E", 9999, 10000] a:["A", "B", "C", "D", "E"]

The above is a small series for you to introduce the JS array operation (array increase, delete, flip, turn string, Fetch index, intercept (slice) slice, splicing splice, array merge), hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.