(10) JavaScript Study Notes-array 1. Create

Source: Internet
Author: User
1. The simplest way to create [javascript] // is to use an array to directly measure varempty []; varprimes [2, 3, 4, 5, 6]; varmisc [1.1, true, & quot; a & quot;]; // The value of the direct array quantity can also be the expression vartable [base, base + 1, base + 2, base + 3]; // It can also contain...

I. Create
[Javascript]
// The simplest method is to use the array directly
Var empty = [];
Var primes = [2, 3, 4, 5, 6];
Var misc = [1.1, true, "a"];
 
// The value of the direct array quantity can also be an expression.
Var table = [base, base + 1, base + 2, base + 3];
 
// It can also contain objects or other Arrays
Var B = [[1, {x: 1, y: 2}], [2, {x: 3, y: 4}];
 
// Undefined elements can be created by ignoring the element values directly with commas
Var count = [1, 3];
Var undefs = [,];
 
// Another way to create an Array is to use Array () to construct a function.
// Call without Parameters
Var a = new Array {};
 
// Explicitly specify the values of the First n elements of the array
Var a = new Array (5, 4, 3, 2, 1, "testing ");
 
// Pass a numeric parameter, specifying the length of the array
Var a = new Array (10 );

Ii. Traverse Arrays
[Javascript]
// Common methods
Var fruits = ["mango", "banana", "cherry", "pear"];
For (var I = 0; I {
Alert (fruits [I]);
}
 
// If there are undefined elements, verify
For (var I = 0; I {
If (fruits [I]) alert (fruits [I]);
}
 
// You can use the same method to initialize Array ()
Var lookup_table = new Array (1024 );
For (var I = 0; I {
Lookup_table [I] = I * 512;
}

3. Array Method
[Javascript]
// Join () method
// Convert all elements in the array into strings
Var a = [1, 2, 3];
Var s = a. join (); // s = "1, 2, 3"
 
// Specify the delimiter
S = a. join ("|"); // s = "1 | 2 | 3"
 
// Reverse () method
// Reverse the order of array elements
Var a = new Array (1, 2, 3 );
A. reverse ();
 
// Sort () method
// Sorts array elements and returns the sorted Array
Var a = new Array ["banana", "cherry", "apple"];
A. sort ();
Var s = a. join (","); // s = "apple, banana, cherry"
 
// If You Want To sort data in another order, you must pass a comparison function as a parameter. If the first parameter is in the second parameter, a value smaller than 0 is returned, then return the number greater than 0, equal return 0
Var a = [33,4, 111,222];
A. sort ();
A. sort (function (a, B)
{
Return a-B;
}
);
 
// Concat () method
// Create and return an array that contains the elements of the original array for calling the method, and add the included parameters to the returned array, but the array cannot be expanded recursively.
Var a = [1, 2, 3];
A. concat (); // returns [, 5]
A. concat ([4, 5]); // returns [1, 2, 3, 4, 5]
A. concat ([], []); // returns [, 7]
A. concat (4, [5, [6, 7]); // returns [1, 2, 4, 5, [6, 7]
 
// Slice () method
// Return a segment of the specified array. The two Parameters specify the start and end points of the segment to be returned.
Var a = [1, 2, 3, 4, 5];
A. slice (0, 3); // returns [1, 2, 3]
A. slice (3); // returns [4, 5
A. slice (1,-1); // returns [2, 3, 4]
A. slice (-3,-2); // returns [3]
 
// Splice () method
// The first parameter specifies the position of the element to be inserted or deleted in the array, and the second parameter specifies the number of elements to be deleted from the array. If the second parameter is omitted, all elements at the end of the array are deleted from the start element.
 
Var a-[1, 2, 3, 4, 5, 6, 7, 8];
A. splice (4); // returns [5, 6, 7, 8]; a is [1, 2, 4]
A. splice (); // returns []; a is []
A. splice (1, 1); // returns [4]; a is [1]
 
// The following parameters specify the elements inserted from the first parameter position.
Var a = [1, 2, 3, 4, 5];
A. splice (, 'A', 'B'); // returns []; a is [, 'A', 'B', 5]
A. splice (, [], 3); // returns ['A', 'B']; a is [, [],]
 
// Push () attaches one or more new elements to the end of the array and returns the new length of the array.
// Pop () deletes one or more elements and returns the deleted Value.
Var stack = [];
Stack. push (1, 2); // stack: [1, 2] returns 2
Stack. pop (); // stack: [1]; returns 2

4. objects similar to Arrays
[Javascript]
// Add attributes to a regular object to make it an object similar to an array, and then traverse the "elements" of the pseudo array"
Var a = {};
Var I = 0;
While (I <10 ){
A [I] = I * I;
I ++;
}
A. length = I;
 
Var total = 0;
For (var j = 0; j totl + = a [j];
}


By: dxh_0829

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.