JavaScript Concise tutorial (3) array

Source: Internet
Author: User
Tags javascript array
JavaScript Array can contain any data type, and access each element by index.
To get the length of the Array, directly access the length property:

[JavaScript] View in plain text

var arr = [1, 2, 3.14, ‘Hello’, null, true];
arr.length; // 6
Please note that directly assigning a new value to the length of the Array will cause the size of the Array to change:

[JavaScript] View in plain text

var arr = [1, 2, 3];
arr.length; // 3
arr.length = 6;
arr; // arr becomes [1, 2, 3, undefined, undefined, undefined]
arr.length = 2;
arr; // arr becomes [1, 2]
Array can modify the corresponding element to a new value by index. Therefore, assigning an index to Array will directly modify this Array:

[JavaScript] View in plain text

var arr = [‘A‘, ‘B’, ‘C’];
arr [1] = 99;
arr; // arr is now [‘A‘, 99, ‘C’]
Please note that if the index exceeds the range when assigning by index, the size of the Array will also change:

[JavaScript] View in plain text


var arr = [1, 2, 3];
arr [5] = ‘x’;
arr; // arr becomes [1, 2, 3, undefined, undefined, ‘x’]
Most other programming languages do not allow the size of the array to be changed directly, and out-of-bounds access to the index will result in an error. However, JavaScript Array will not have any errors. When writing code, it is not recommended to directly modify the size of the Array. When accessing the index, make sure that the index does not cross the boundary.

indexOf
Similar to String, Array can also search the position of a specified element through indexOf ():

[JavaScript] View in plain text

var arr = [10, 20, ‘30’, ‘xyz’];
arr.indexOf (10); // The index of element 10 is 0
arr.indexOf (20); // The index of element 20 is 1
arr.indexOf (30); // element 30 is not found, returns -1
arr.indexOf (‘30 ’); // The index of element‘ 30 ’is 2
Note that the number 30 and the character string ‘30’ are different elements.

slice
slice () is the substring () version of the corresponding String, which intercepts some elements of the Array, and then returns a new Array:

[JavaScript] View in plain text
var arr = [‘A‘, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’];
arr.slice (0, 3); // Starting from index 0 and ending at index 3, but excluding index 3: [‘A‘, ‘B’, ‘C’]
arr.slice (3); // From index 3 to the end: [‘D‘, ‘E’, ‘F’, ‘G’]
Note that the start and end parameters of slice () include the start index and not the end index.

If you don't pass any parameters to slice (), it will intercept all the elements from beginning to end. Using this, we can easily copy an Array:

[JavaScript] View in plain text

var arr = [‘A‘, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’];
var aCopy = arr.slice ();
aCopy; // [‘A‘, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’]
aCopy === arr; // false
push and poppush () add several elements to the end of the Array, and pop () deletes the last element of the Array:

[JavaScript] View in plain text

var arr = [1, 2];
arr.push (‘A‘, ‘B’); // returns the new length of Array: 4
arr; // [1, 2, ‘A‘, ‘B‘]
arr.pop (); // pop () returns ‘B’
arr; // [1, 2, ‘A‘]
arr.pop (); arr.pop (); arr.pop (); // pop 3 times in a row
arr; // []
arr.pop (); // Continue pop with empty array will not report error, but return undefined
arr; // []


unshift and shift If you want to add some elements to the head of the Array, use the unshift () method. The shift () method deletes the first element of the Array:

[JavaScript] View in plain text

var arr = [1, 2];
arr.unshift (‘A‘, ‘B’); // returns the new length of Array: 4
arr; // [‘A‘, ‘B’, 1, 2]
arr.shift (); // ‘A’
arr; // [‘B‘, 1, 2]
arr.shift (); arr.shift (); arr.shift (); // shift 3 times in a row
arr; // []
arr.shift (); // Continue to shift with an empty array will not report an error, but return undefined
arr; // []


sortsort () can sort the current Array, it will directly modify the element position of the current Array, when directly called, sort in the default order:

[JavaScript] View in plain text

var arr = [‘B‘, ‘C’, ‘A’];
arr.sort ();
arr; // [‘A‘, ‘B’, ‘C’]


Can it be sorted in the order we specified? Absolutely, we will talk about it in the following function.

 

 

reversereverse () removes the elements of the entire Array, which is reversed:

[JavaScript] View in plain text

var arr = [‘one‘, ‘two’, ‘three’];
arr.reverse ();
arr; // [‘three‘, ‘two’, ‘one’]


splice

The splice () method is a "universal method" to modify Array. It can delete several elements from the specified index, and then add several elements from this position:
[JavaScript] View in plain text

var arr = [‘Microsoft‘, ‘Apple’, ‘Yahoo’, ‘AOL’, ‘Excite’, ‘Oracle’]; // Remove three elements from index 2, then add two more elements:
arr.splice (2, 3, ‘Google’, ‘Facebook’); // return deleted elements [‘Yahoo’, ‘AOL’, ‘Excite’]
arr; // [‘Microsoft‘, ‘Apple’, ‘Google’, ‘Facebook’, ‘Oracle’]
// Only delete, not add:
arr.splice (2, 2); // [‘Google’, ‘Facebook’]
arr; // [‘Microsoft‘, ‘Apple’, ‘Oracle’]
// Only add, not delete:
arr.splice (2, 0, ‘Google’, ‘Facebook’); // returns [], because no elements are deleted
arr; // [‘Microsoft‘, ‘Apple’, ‘Google’, ‘Facebook’, ‘Oracle’]


concat
The concat () method connects the current Array with another Array and returns a new Array:
[JavaScript] View in plain text

var arr = [‘A‘, ‘B’, ‘C’];
var added = arr.concat ([1, 2, 3]);
added; // [‘A‘, ‘B’, ‘C’, 1, 2, 3]
arr; // [‘A‘, ‘B’, ‘C’]
Please note that the concat () method does not modify the current Array, but returns a new Array.
In fact, the concat () method can receive any number of elements and Array, and automatically disassemble the Array, and then add all to the new Array:
[JavaScript] View in plain text

var arr = [‘A‘, ‘B’, ‘C’];
arr.concat (1, 2, [3, 4]); // [‘A‘, ‘B‘, ‘C’, 1, 2, 3, 4]


join
The join () method is a very practical method. It connects each element of the current Array with the specified string, and then returns the connected string:
[JavaScript] View in plain text

var arr = [‘A‘, ‘B’, ‘C’, 1, 2, 3];
arr.join (‘-‘); // ‘A-B-C-1-2-3’
If the elements of the Array are not strings, they will be automatically converted to strings and then connected.
http://www.ququer.org/
sodu novel
Javascript concise tutorial (3) array

Label: modify medium border highlight cin pass hello search substring

Original address: http://www.cnblogs.com/yaogua/p/6623934.html
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.