Clear, define, and delete javascript Array Operations

Source: Internet
Author: User

Js arrays have various operations. Next I will introduce how to clear, define, and delete js arrays, and also introduce array sorting. You are welcome to refer to them.

Let's take a look at how to clear the js array.


When an array needs to be cleared, many people use the following method:

A = [];

We know that javascript variable storage methods can be divided into reference types and direct quantities. The array belongs to the object, that is, the reference type. It references the variable pointer address, which is designed to save memory.


In addition, if the preceding empty array method is used to assign a new array directly, the previously referenced array may not be released (with other references ), for example, the following code:

The Code is as follows: Copy code


Var a = [2, 3];
Var B =;
A = [];
Console. log (B );

At this time, a and B are not the same array. After a is cleared, B still refers to the referenced address. unless you mean it, this will pose a risk.

Therefore, the best way to empty the array is to set the length to 0, that is:

 

The Code is as follows: Copy code
A. length = 0;


Create and access js Arrays


Array Creation
 

The Code is as follows: Copy code

Var arrayObj = new Array (); // create an Array

Var arrayObj = new Array ([size]); // create an Array and specify the length. Note that the length is not the upper limit.

Var arrayObj = new Array ([element0 [, element1 [,... [, elementN]); // create an Array and assign values

 
 
It should be noted that, although the second method creates an array with a specified length, the array actually gets longer in all cases, that is, even if the length is 5, you can still store elements outside the specified length. Note: The length will change accordingly.

2. Access to array elements

The Code is as follows: Copy code

 
Var testGetArrValue = arrayObj [1]; // obtain the element value of the array.

ArrayObj [1] = "this is a new value"; // assign a new value to the array element

 
 

3. Add array elements

 

The Code is as follows: Copy code

Code
ArrayObj. push ([item1 [item2 [... [itemN]); // Add one or more new elements to the end of the array and return the new length of the array.

ArrayObj. unshift ([item1 [item2 [... [itemN]); // adds one or more new elements to the array. The elements in the array are automatically removed and the new length of the array is returned.

ArrayObj. splice (insertPos, 0, [item1 [, item2 [,... [, itemN]); // insert one or more new elements to the specified position of the array. The inserted element is automatically removed and "" is returned "".

Js array Conversion


1. convert an array to a string (concatenate an array element into a string using a character)

The Code is as follows: Copy code

Var a, B;

A = new Array (0, 1, 2, 3, 4 );

B = a. join ("-");

 

2. convert a string to an array (cut the string into several strings based on a specific character and return the string as an array)

The Code is as follows: Copy code

Var s = "abc, abcd, aaa ";

Ss = s. split (","); // separate each comma.


Delete array elements


Var arr = ['A', 'B', 'C'];

To delete 'B', you can use either of the following methods:

1. delete method: delete arr [1]

In this way, the length of the array remains unchanged. In this case, the arr [1] is changed to undefined, but the index of the original array remains unchanged. In this case, you need to traverse the array element before using it.

The Code is as follows: Copy code
For (index in arr)
{
Document. write ('Arr ['+ index +'] = '+ arr [index]);
}

 

This Traversal method skips the undefined element.

// It is often used to reconstruct the array through traversal.
 

The Code is as follows: Copy code

Array. prototype. remove = function (dx)
{
If (isNaN (dx) | dx> this. length) {return false ;}
For (var I = 0, n = 0; I <this. length; I ++)
{
If (this [I]! = This [dx])
{
This [n ++] = this [I]
}
}
This. length-= 1
}
A = ['1', '2', '3', '4', '5'];
Alert ("elements:" + a + "nLength:" + a. length );
A. remove (0); // Delete the element whose subscript is 0
Alert ("elements:" + a + "nLength:" + a. length );


Array common functions

ToString (): converts an array into a string.
ToLocaleString (): converts an array into a string.
Join (): converts an array into a string connected with symbols.
Shift (): removes an element from the array header.
Unshift (): insert an element in the header of the array.
Pop (): deletes an element from the end of the array.
Push (): Add an element to the end of the array.
Concat (): add elements to the array
Slice (): returns the part of the array
Reverse (): sorts arrays in reverse order.
Sort (): sorts arrays.
Splice (): insert, delete, or replace an array element.

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.