On the usage _javascript skill of JS array and Splice

Source: Internet
Author: User

First add a splice function:

Splice: The purpose of this method is to remove an element from an array

Array.splice (Index,count,value ...);

Index: Indicates from which subscript to start,

Count: Indicates the number of deleted elements

Value: Represents the added element

Example

1.var array = new Array (1,2,3,4,5,6);

Array.splice (0,1,2)

result:2,2,3,4,5

2.var array = new Array (1,2,3,4,5,6);

Array.splice (0,0,2)
result:2,1,2,3,4,5

3.var array = new Array (1,2,3,4,5,6);

Array.splice (0,0,2,3,4)

2,3,4,1,2,3,4,5,6

1, the creation of the array

var arr = new Array (); Arr[0] = "AAA"; arr[1] = "BBB"; arr[2] = "CCC";

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

var c = new Array ("A", "second", "third");

or the direct amount of the array:

var d = ["A", "second", "third"];

var arrayobj = new Array (); Create an array of
var arrayobj = new Array ([size]);//Create an array and specify the length, note that is not the upper limit, is the length
var arrayobj = new Array ([element0[, element1[, ... [, ELEMENTN]]]); Create an array and assign a value

To illustrate, although the second method creates an array that specifies the length, in all cases the array is longer, that is, even if you specify a length of 5, you can still store the elements outside the specified length, note: the length changes.

2, access to elements of the array

var testgetarrvalue=arrayobj[1]; Gets the element value of the array
arrayobj[1]= "This is the new value";//give the array element a new value

3, the addition of array elements

Arrayobj. Push ([Item1 [item2 [...] [Itemn]]]); /Adds one or more new elements to the end of the array and returns an array of new length
arrayobj.unshift ([Item1 [item2]. [Itemn]]]); /Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned
Arrayobj.splice (insertpos,0,[item1[, item2[, ...). [, Itemn]]]); /inserts one or more new elements into the array at the specified position, and the element at the insertion point is automatically moved back to "".

4, the deletion of the elements of the array

var a = new Array ("A", "second", "third");  Delete A[1]; The document.write (a.length)//Display result is 3, indicating that the length of the array cannot be changed even if deleted

Arrayobj.pop (); Remove the last element and return the element value
arrayobj.shift ();//Remove the first element and return the element value, and the elements in the array are automatically moved forward
Arrayobj.splice (deletepos,deletecount); Deletes the specified number of DeleteCount elements starting at the specified position, deletepos the removed elements

5, the array of interception and merging

Arrayobj.slice (start, [end]); Returns a portion of an array as an array, noting that the end-corresponding element is not included, and if omitting the end will replicate all elements arrayobj.concat after start
([item1[, item2[, ...). [, Itemn]]]); Concatenate multiple arrays (or a string, or a mixture of arrays and strings) into an array, returning a new array of connections

6, the copy of the array

Arrayobj.slice (0); Returns an array of copies, noting a new array, not pointing to
arrayobj.concat ();//Returning an array of copies, note that a new array is not pointing

7, the ordering of array elements

Arrayobj.reverse (); Reverse elements (top to last, last row to top), return array address
arrayobj.sort ();//////array element sort

8. String of array elements

Arrayobj.join (separator); Returns a string that connects each element value of an array, separated by a separator in the middle.
tolocalestring, toString, valueof: Can be seen as a special use of join, not commonly used

This article on the JS array and the use of splice 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.