Summary of array usage in Javascript

Source: Internet
Author: User
Tags javascript array

<Script language = "JavaScript">
// Author: dongge
// Date: 2008-1-11
// Objective: to perform basic operations on Arrays

/*
Javascript is a non-type language, so an array element can have any data type, different elements of the same Array
The array element settings can contain other arrays, so that you can create a complex array.
At this point, JavaScript, as a scripting language, is different from the strict object-oriented C ++. C # And java. It has higher flexibility.
*/

/*
* In javascript1.1 and later versions, arrays are created using constructor array () and operator new,
The following three methods can be used to create arrays in javascript.
*/
VaR A = new array ();
VaR B = new array (5, 4, 3, "first", "test, string ");
VaR c = new array (20 );

A [1.23] = "test ";
Document. Write ("A [1.23] =" + A [1.23]);
// Believe that each of the strongly typedProgramming LanguageWhen I was learning JavaScript, I was surprised by the above operations,
// Float data is also used as the subscript of the array. In fact, it is not as you think.
// JavaScript converts a negative number, floating point number (or Boolean, object, or other value) to a string.
// Use the generated string as the property name of the object, instead of defining a new array element.
// The above instance is actually creating an attribute named "1.23" for.
Document. Write ("A. Length =" + A. Length );
Document. Write ("B. Length =" + B. Length );
Document. Write ("C. Length =" + C. Length );

A [3] = "test ";
Document. Write ("<br/> A [3] =" + A [3]);
Document. Write ("<br/> A. Length =" + A. Length );
// The above test also makes it clear that we use an integer as the subscript of the array to actually add an element to the array,
// Here the length of the array is used to reflect the mysteries of the Javascript array.

// The length of the array can be truncated by setting the Length attribute of the array.
. length = 3;
if (a [3] = undefined)
{< br> document. write ("
in. length = "+. after Length + ", a [3] =" + A [3]);
}< br> else
{< br> document. write ("
in. length = "+. after Length + ", a [3] =" + A [3]);
}

// test our multi-dimensional array elements here
/*
* in Javascript, multi-dimensional arrays are not supported.
* However, we use a one-dimensional array the element is assigned a one-dimensional array, in this way, the multi-dimensional array is implemented, but
it is actually a one-dimensional array, this is the same idea as we understand the array of C language , but their implementation mechanism is different.
*/
var G = new array (3);
G [3] =;
G [3] [2] = "test"
document. write ("
G [3] [2] =" + G [3] [2]);
// array join () method
for (VAR I = 0; I <20; I ++)
{< br> C [I] = I;
document. write ("
C [I] =" + C [I]);
}< br> document. after the write ("
C element join () method is:" + C. join ();
// reverse () method of the array
C. reverse ();
document. the result of the write ("
C element after the reverse () method and then join () is:" + C. join ("|");

// Test the Concat () method
VaR H = new array (1, 2, 3 );
H = H. Concat ([4, 5]);
// But the Concat function does not recursively expand an array whose elements are arrays.
H = H. Concat (6, 7, [9, [10, 20]);
Document. Write ("<br/> H. Length =" + H. Length + "<br/>" + H );
Document. Write ("H [8] =" + H [8]);

// Slice () method
Document. Write ("<br> H. Slice (4,5) =" + H. Slice (4,5 ));
Document. Write ("H. Slice (5, 9) =" + H. Slice (5, 9 ))
// Slice () method: the returned array contains the element specified by the first parameter and the element specified by the second parameter.
// The element that ends with the element but does not contain the element specified by the second parameter.

// Splice () method
// The splice () method is a common method used to insert or delete array elements.
/*
The first parameter of the splice function specifies the position of the element to be inserted or deleted in the array.
The second parameter specifies the number of elements to be deleted from the array.
After the second parameter, there can be any number of parameters that specify the elements inserted from the position specified by the first parameter.
Move the first element and subsequent elements accordingly.
*/

Document. Write ("<br/> H. After splice (8, 1), H is:" + H. splice (8, 1 ));
// Document. write ("<br/> H. h after splice (8, 0, 'A', 'B', 'test') is: "+ H. splice (8, 0, 'A', 'B', 'test '));
H. splice (7,0, 'A', 'B', 'test ');
Document. Write ("<br/> H. splice (, 'A', 'B', 'test') after H:" + H );

// The array in Javascript is used as a stack and is similar to that in PHP.
// This is more interesting and useful.
// The following is a small instance used as a stack
/*
The push method attaches one or more new elements to the end of the array and returns the new length of the array.
Pop will delete the last element of the array, stick to the length of the array, and return the deleted Value.
*/
VaR stack = new array ();
Stack. Push (1, 2 );
Document. Write ("<br> the stack element is:" + stack );
Document. Write ("<br/> stack. Length =" + stack. Length );
Document. Write ("<br> the result returned by stack. Pop () is:" + stack. Pop ());
Document. Write ("<br/> stack. Length =" + stack. Length );

// The following is a small instance used as a queue
/*
The unshift method adds one or more elements to the header of the array element, and moves the existing elements to the largest position of the subscript to free up space.
It returns the new length of the main family.
Shift is to delete and return the first element of the array, and then move all the following elements forward to fill the blank left by the first element.
*/
VaR list = [];
List. unshift (6, 2 );
Document. Write ("<br> the content of the list is:" + list );
Document. Write ("<br> the shift method of list is:" + list. Shift ());

// The rest is the tostring () method we are familiar with in Java.
// It's a piece of cake!
Document. Write (C. tostring ());
// To put it bluntly, the tostring () method of the array has the same effect as the join () method without parameters.
// OK, this's chapter for array, that's all!

</SCRIPT>

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.