JavaScript array Array object addition and deletion element method summarizing _javascript techniques

Source: Internet
Author: User
Tags array length javascript array

The example in this article summarizes the method of adding and removing elements of JavaScript array array objects. Share to everyone for your reference. The specific analysis is as follows:

Pop method

Moves the last element in the divisor group and returns the element.
Arrayobj.pop ()
The required arrayobj reference is an Array object.
Description
If the array is empty, then the undefined is returned.

Shift method

Moves the first element in the divisor group and returns the element.
Arrayobj.shift ()
The required arrayobj reference is an Array object.
Description
The shift method moves the first element in the divisor group and returns the element.

Copy Code code as follows:
var arr = new Array (0,1,2,3,4);
var remove = Arr.pop ();
alert (remove);
alert (arr.length);


Remove and return the last element, first eject 4, and then prompt the current array length pop-up 4!

Push Method

Adds a new element to an array and returns the new length value of the array.
Arrayobj.push ([Item1 [item2 [...] [Itemn]]])
Parameters
Arrayobj
Required option. An Array object.
Item, Item2,... itemn
Options available. The new element of the Array.
Description
The push method adds these elements in the order in which the new elements appear. If one of the arguments is an array, the array is added as a single element to the arrays. If you want to merge elements from two or more arrays, use the Concat method.

Copy Code code as follows:
var arr = new Array (0,1,2,3,4);
parameter is one or more
var len = Arr.push (5,6);
Len = Arr.push (7);
for (Var i=0;i<arr.length;i++) {
Alert (Arr[i]);
}


You can add multiple at once, or you can add one to return the current length of the array. Changed the print array content observation changes!

Splice method

Removes one or more elements from an array and, if necessary, inserts a new element at the position of the removed element, returning the removed element.
Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]
Parameters
Arrayobj
Required option. An Array object.
Start
Required option. Specifies that the starting position of the element is removed from the array, which is calculated starting at 0.
DeleteCount
Required option. The number of elements to remove.
Item1, item2,.. ., itemn
Required option. The new element to insert at the location of the removed element.
Description
The splice method modifies arrayobj by removing the specified number of elements starting from the start position and inserting new elements. The return value is a new Array object that consists of the removed element.

Copy Code code as follows:
var arr = new Array (0,1,2,3,4);
Delete two elements starting from 2, position starting from 0
Returns an array of removed elements
var Rearr = Arr.splice (2,2);
You can replace the new element in the location where the element is removed.
Just add new elements from the start position, and if you remove two elements, you can add up to 10 new elements.
var Rearr = Arr.splice (2,2,6,7,8,9);
for (Var i=0;i<arr.length;i++) {
Alert (Arr[i]);
}


If you don't want to add new elements, don't pass the third argument!

Concat Method (Array)

Returns a new array, which is a combination of two or more arrays.
Array1.concat ([item1[, item2[, ...) [, Itemn]]]
Parameters
Array1
Required option. The array object to which all other arrays are to be connected.
Item1,.. ., itemn
Options available. Other items to connect to the end of Array1.
Description
The Concat method returns an Array object that contains a connection to the array1 and any other items provided.
Items to add (item1 ... itemn) are added to the array in Left-to-right order. If an item is an array, add its contents to the end of the array1. If the item is not an array, it is added to the end of the array as a single array element.

The following is a copy of an element from the source array to the result array:

For object parameters copied from the array being connected to the new array, the same object is still pointed to after replication. Whatever changes in the new array and source array will cause another change.
For a numeric or string connection to a new array, only its value is copied. A change in the value of an array does not affect the value in the other array.

Copy Code code as follows:
var arr = new Array (0,1);
var arr2 = new Array (3,4);
var arr = Arr.concat (ARR2);
for (Var i=0;i<arr.length;i++) {
Alert (Arr[i]);
}


The function of the method is to copy the elements in the arr2 into the arr!

I hope this article will help you with your JavaScript programming.

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.