Delete array elements from the JS array!

Source: Internet
Author: User
1. Create an array object

Let's introduce how to create an array object of JavaScript.CubeSyntax:

1. When declaring an array, only several components in the array are declared.

VaR array object name = new array (number of components );

Fruit = new array (3); // declare an array named fruit. There are three components, which is equivalent to three variables declared at a time.

Then you must prepare several additional lines.ProgramCodeAnd enter the variable values in the descending order.

Fruit [0] = "watermelon ";
Fruit [1] = "apple ";
Fruit [2] = "banana ";

2. All array components are directly given during Declaration. They are separated by commas and enclosed in parentheses. The number of components is the length of the array.

VaR array object name = new array (component 1..., component N );

VaR fruit = new array ("watermelon", "apple", "banana ");

Note: In general languages, components in a number group must be of the same type, but different types of data can be put into arrays in JavaScript.

2. attributes of an array object

Javascript provides the following attributes for array objects:

Format:
Array object name. Attribute

Description
1 constructor specifies the prototype Function
2. index indicates the index value of the array component.
3. Input indicates the string in the Rule expression.
4 length: Obtain the array length (number of array components ).
5 prototype is used to create custom object attributes.

3. array object Method

Javascript provides the following methods for array objects:

Format:
Array object name. Method (parameter)

Description
1 Concat (array 1, array 2,..., array n) combines multiple arrays into a new array
2 join (separator) combines an array into a string, separated by a specific character
3 POP () deletes the last component in the array and returns the component content.
4 push (component 1, component 2,..., component n) add one or more components to the end of the array and return the content of the last component.
5 reverse () Reverse the index order of all components in the array (transpose)
The first component changes to the last one, and the last component gets the first one.
6 shift () deletes the first component in the array and returns the component content.
7. Slice (STARTS indexing and ends indexing) transfers the array content to a new PostgreSQL
8 sort () sorts the array content
9 splice () add or delete array Components
10 tosource () returns an array constant representing a specific array and can be used to create a new array.
11 tostring () represents the array and its components in a string
12 unshift (component 1, component 2,..., component n) add one or more components to the beginning of the array and return the length of the last array.
13 valueof () Get the array value

Note: Some methods, such as push, shift, unshift ...... Some versions of Internet Explorer do not support this feature. Pay special attention to this feature.

Example:

/*************************************** *******************/
JS deletes array elements:

< Script Language = " Javascript " Type = " Text/JavaScript " >
Array. Prototype. DEL = Function (N) { // N indicates the number of items, starting from 0.
// Prototype is the object prototype. Note that the custom method is added for the object.
If (N < 0 ) // If n is less than 0, no operation is performed.
Return This ;
Else
Return This . Slice ( 0 , N). Concat ( This . Slice (n + 1 , This . Length ));
/**/ /*
Concat method: returns a new array consisting of two or more arrays.
Here we return this. Slice (0, N)/This. Slice (n + 1, this. length)
In the middle of the new array, the nth entry is missing.
Slice Method: return a segment of an array with two parameters, respectively specifying the start and end positions.
*/
}
// Let's try this method we have added.
VaR Test = New Array ( 0 , 1 , 2 , 3 , 4 , 5 );
Test = Test. Del ( 3 ); // The value starts from 0. In this example, 4th items are deleted.
Alert (test );
</ Script >

/*************************************** *********************/
. An important feature of JS array that is different from other languages is that it can contain different data types in different rows. This is also a feature of weak type languages such as Js!

}

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.