Summary of adding and deleting elements to and from Array objects in JavaScript Array, javascriptarray

Source: Internet
Author: User

Summary of adding and deleting elements to and from Array objects in JavaScript Array, javascriptarray

This example summarizes how to add and delete elements to and from a JavaScript Array object. Share it with you for your reference. The specific analysis is as follows:

Pop Method

Removes the last element from the array and returns the element.
ArrayObj. pop ()
The required arrayObj reference is an Array object.
Description
If this array is empty, undefined is returned.

Shift Method

Removes the first element from the array and returns the element.
ArrayObj. shift ()
The required arrayObj reference is an Array object.
Description
The shift method removes the first element from the array and returns the element.
Copy codeThe Code is 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. 4 is displayed first, and 4 is displayed!
 
Push method

Add the new element to an array and return the new Length Value of the array.
ArrayObj. push ([item1 [item2 [... [itemN])
Parameters
ArrayObj
Required. An Array object.
Item, item2,... itemN
Optional. The new element of the Array.
Description
The push method adds new elements in the order they appear. If one of the parameters is an array, the array will be added to the array as a single element. To merge two or more elements in an array, use the concat method.
Copy codeThe Code is as follows: var arr = new Array (0, 1, 2, 3, 4 );
// The 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 entries at a time or add one to return the current length of the array. Changed the print array content to observe the changes!

Splice Method

Remove one or more elements from an array. If necessary, insert a new element to the position of the removed element to return the removed element.
ArrayObj. splice (start, deleteCount, [item1 [, item2 [,... [, itemN])
Parameters
ArrayObj
Required. An Array object.
Start
Required. Specifies the start position of the element to be removed from the array. This position is calculated from 0.
DeleteCount
Required. The number of elements to be removed.
Item1, item2,..., itemN
Required. The new element to be inserted at the position of the removed element.
Description
The splice method can remove the specified number of elements starting from the start position and insert new elements to modify arrayObj. The returned value is a new Array object consisting of the removed elements.
 
Copy codeThe Code is as follows: var arr = new Array (0, 1, 2, 3, 4 );
// Delete two elements starting from 2 and starting from 0
// Returns the array of elements to be removed.
Var reArr = arr. splice (2, 2 );
// You can replace the new element at the position where the element is removed.
// Add new elements from the beginning of the removal. If you remove two elements, you can add 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 do not want to add new elements, do not pass the third parameter!

Concat method (Array)

Returns a new array consisting of two or more arrays.
Array1.concat ([item1 [, item2 [,... [, itemN])
Parameters
Array1
Required. The Array object to be connected to all other arrays.
Item1,..., itemN
Optional. To connect to another project at the end of array1.
Description
The concat method returns an Array object that contains the connection between array1 and any other projects provided.
Project to be added (item1... ItemN) is added to the array from left to right. If an item is an array, add its content to the end of array1. If the project is not an array, add it as a single array element to the end of the array.

Copy elements from the source array to the result array as follows:

For the object parameters copied from the array being connected to the new array, the Copied object still points to the same object. Any change in the new array and source array will lead to another change.
Only the values of the values or strings connected to the new array are copied. Changing the value of a group does not affect the value of another array.
Copy codeThe Code is 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 arr2 To arr!

I hope this article will help you design javascript programs.

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.