Introduction to JavaScriptArray object_basic knowledge

Source: Internet
Author: User
This article mainly introduces the related information of the JavaScriptArray object. For more information, see Array

1. Introduction

An array is an ordered set of values. Each value is called an element, and each element has a position in the array, which is represented by numbers and is called an index. JavaScript arrays are non-typed: array elements can be of any type, and different elements in the same array may have different types. -- JavaScript authoritative guide (Version 6)

2. definition

The code is as follows:


Var names = new Array ("zhang san", "Li Si", "Wang Wu ");
// Or
Var names = ["zhang san", "Li Si", "Wang Wu"];

3. attributes

Length: the length of elements in the array.

4. instance method

Common methods:

1) unshift (): insert an element in the array header

2) shift (): removes and returns the first element of the array.

3) push (): insert an element at the end of the array

4) pop (): removes and returns the last element of the array.

4.1 concat (): concatenates an element into an array. The original array is not modified, and a new array is returned.
Parameters:

① Value1, value2...... valueN: any number of values

Return value:

{Array} is a new Array that contains the original Array and new elements.

Example:

The code is as follows:


Var demoArray = ['A', 'B', 'C'];
Var demoArray2 = demoArray. concat ('e ');
Console. log (demoArray); // => demoArray: ['A', 'B', 'C'] the original array is not changed.
Console. log (demoArray2); // => ['A', 'B', 'C', 'E']

4.2 every (): traverse the elements in sequence to determine whether each element is true.
Parameters:

① Function (value, index, self) {}: each element uses this function to determine whether it is true. when it is determined that it is false, the traversal ends immediately.

Value: Elements of array traversal.

Index: element number

Self: Array itself

Return value:

{Boolean}: true is returned only when each element is true. if one element is false, false is returned.

Example:

The code is as follows:


Var demoArray = [1, 2, 3];
Var rs = demoArray. every (function (value, index, self ){
Return value> 0;
});
Console. log (rs); // => true

4.3 filter (): traverses elements in sequence and returns a new array containing elements that meet the conditions.
Parameters:

① Function (value, index, self) {}: each element calls this function in turn and returns a new array containing the elements that meet the conditions.

Value: Elements of array traversal.

Index: element number

Self: Array itself

Return value:

{Array} a new Array containing qualified elements

Example:

The code is as follows:


Var demoArray = [1, 2, 3];
Var rs = demoArray. filter (function (value, index, self ){
Return value> 0;
});
Console. log (rs); // => [1, 2, 3]

4.4 forEach (): traverses the elements in sequence and executes the specified function. no return value is returned.
Parameters:

① Function (value, index, self) {}: each element calls this function in turn

Value: Elements of array traversal.

Index: element number

Self: Array itself

Return value: None

Example:

The code is as follows:


Var demoArray = [1, 2, 3];
DemoArray. forEach (function (value, index, self ){
Console. log (value); // => output in sequence: 1 2 3
});

4.5 indexOf (): Searches for matching elements in the array. If no matching element exists,-1 is returned. When searching, use the "=" operator. Therefore, distinguish between 1 and '1'
Parameters:

① Value: the value to be searched in the array.

② Start: the position of the sequence number to start searching. if it is omitted, it is 0.

Return value:

{Int}: returns the sequence number of the first matched value in the array. If no value exists,-1 is returned.

Example:

The code is as follows:


['A', 'B', 'C']. indexOf ('A'); // => 0
['A', 'B', 'C']. indexOf ('A', 1); // =>-1
['A', 'B', 'C']. indexOf ('D'); // =>-1
[1, 2, 3]. indexOf ('1'); // =>-1: match with '='

4.6 join (): concatenates all elements in the array into a string using a separator.
Parameters:

① Sparator {String}: the separator between each element. if omitted, it is separated by commas (,) by default.

Return value:

{String}: a String concatenated by sparator.

Example:

The code is as follows:


['A', 'B', 'C']. join (); // => 'A, B, C'
['A', 'B', 'C']. join ('-'); // => 'A-B-C'

4.7 lastIndexOf: reverse searches for matching elements in the array. If no matching element exists,-1 is returned. When searching, use the "=" operator. Therefore, distinguish between 1 and '1'
Parameters:

① Value: the value to be searched in the array.

② Start: the position of the sequence number to start searching. If this parameter is omitted, the sequence number is searched from the last element.

Return value:

{Int}: searches for the sequence number of the first matched value in the array from right to left. If no value exists,-1 is returned.

Example:

The code is as follows:


['A', 'B', 'C']. lastIndexOf ('A'); // => 0
['A', 'B', 'C']. lastIndexOf ('A', 1); // => 0
['A', 'B', 'C']. lastIndexOf ('D'); // =>-1
[1, 2, 3]. lastIndexOf ('1'); // =>-1: Use '=' to match

4.8 map (): Traverse and calculate each element in sequence, and return the array of the calculated elements.
Parameters:

① Function (value, index, self) {}: each element calls this function in sequence and returns the computed element.

Value: Elements of array traversal.

Index: element number

Self: Array itself

Return value:

{Array} a new Array containing even good elements

Example:

The code is as follows:


[1, 2, 3]. map (function (value, index, self ){
Return value * 2;
}); // => [2, 4, 6]

4.9 pop (): removes and returns the last element of the array.
Parameter: None

Return value:

The last element of the {Object} array. If the array is empty, undefined is returned.

Example:

The code is as follows:


Var demoArray = ['A', 'B', 'C'];
DemoArray. pop (); // => c
DemoArray. pop (); // => B
DemoArray. pop (); // =>
DemoArray. pop (); // => undefined

4.10 push (): add the element to the end of the array
Parameters:

① Value1, value2...... valueN: add any number of values to the end of the array.

Return value:

New length of {int} array

Example:

The code is as follows:


Var demoArray = ['A', 'B', 'C'];
DemoArray. push ('D'); // => 4, demoArray: ['A', 'B', 'C', 'D']
DemoArray. push ('e', 'F'); // => 6, demoArray: ['A', 'B', 'C', 'D', 'e ', 'F']
Console. log (demoArray); // => ['A', 'B', 'C', 'D', 'e', 'F']

4.11 reverse (): reverse the order of array elements.
Parameter: None

Returned value: None (reverse element order in the original number Group ).

Example:

The code is as follows:


Var demoArray = ['A', 'B', 'C', 'D', 'E'];
DemoArray. reverse ();
Console. log (demoArray); // => ["e", "d", "c", "B", "a"]

4.12 shift (): Remove and return the first element of the array
Parameter: None

Return value:

The first element of the {Object} array. If the array is empty, undefined is returned.

Example:

The code is as follows:


Var demoArray = ['A', 'B', 'C'];
DemoArray. shift (); // =>
DemoArray. shift (); // => B
DemoArray. shift (); // => c
DemoArray. shift (); // => undefined

4.13 slice (startIndex, endIndex): return part of the array.
Parameters:

① StartIndex: sequence number at the beginning. if it is a negative number, it indicates that the calculation starts from the end.-1 indicates the last element,-2 indicates the second to last, and so on.

② EndIndex: The last sequence number of the end element. if it is not specified, it is the end. The intercepted element does not contain the element of the sequence number. it ends with the first element of the sequence number.

Return value:

{Array} is a new Array that contains all elements of the previous element from startIndex to endIndex.

Example:

The code is as follows:


[1, 2, 3, 4, 5, 6]. slice (); // => [1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]. slice (1); // => [2, 3, 4, 5, 6]: capture from sequence 1
[1, 2, 3, 4, 5, 6]. slice (0, 4); // => [1, 2, 3, 4]: Intercept the elements from number 0 to number 3 (first of number 4)
[1, 2, 3, 4, 5, 6]. slice (-2); // => [5, 6]: truncates two elements

4.14 sort (opt_orderFunc): sort by certain rules
Parameters:

① Opt_orderFunc (v1, v2) {Function}: an optional sorting rule Function. If omitted, the elements are sorted in ascending order by letters.

V1: the element before the traversal.

V2: elements after the time.

Sorting rules:

Compare v1 and v2, and return a number to represent the v1 and v2 sorting rules:

Smaller than 0: v1 is smaller than v2, and v1 is at the top of v2.

Equal to 0: v1 is equal to v2, and v1 is placed before v2.

Greater than 0: v1 is greater than v2, and v1 is placed behind v2.

Returned value: None (sorting in the original array ).

Example:

The code is as follows:


[1, 3, 5, 2, 4, 11, 22]. sort (); // => [1, 11, 2, 22, 3, 4, 5]
[1, 3, 5, 2, 4, 11, 22]. sort (function (v1, v2 ){
Return v1-v2;
}); // => [1, 2, 3, 4, 5, 11, 22]: sorting from small to large
[1, 3, 5, 2, 4, 11, 22]. sort (function (v1, v2 ){
Return-(v1-v2); // you can convert it from large to small.
}); // => [22, 11, 5, 4, 3, 2, 1]

4.15 splice (): insert or delete array elements
Parameters:

① Start {int}: start sequence number for start insertion, deletion, or replacement.

② DeleteCount {int}: number of elements to be deleted, which is calculated from start.

③ Value1, value2... valueN {Object}: an optional parameter that indicates the element to be inserted and is inserted from start. If the ② parameter is not 0, the delete operation is performed first, and then the insert operation is performed.

Return value:

{Array} returns a new Array containing the deleted elements. If the ② parameter is 0, no elements are deleted, and an empty array is returned.

Example:

The code is as follows:


// 1. delete
Var demoArray = ['A', 'B', 'C', 'D', 'E'];
Var demoArray2 = demoArray. splice (0, 2); // delete two elements starting from 0 and return an array containing the deleted elements: ['A', 'B']
Console. log (demoArray2); // => ['A', 'B']
Console. log (demoArray); // => ['C', 'D', 'E']
// 2. Insert
Var demoArray = ['A', 'B', 'C', 'D', 'E'];
Var demoArray2 = demoArray. splice (0, 0, '1', '2', '3'); // The ② parameter is 0 and an empty array is returned.
Console. log (demoArray2); // => []
Console. log (demoArray); // => ['1', '2', '3', 'A', 'B', 'C', 'D ', 'E']
// 3. delete and insert
Var demoArray = ['A', 'B', 'C', 'D', 'E'];
// When parameter ② is not 0, delete the four elements whose sequence number starts from 0 and return an array containing the deleted elements.
Var demoArray2 = demoArray. splice (0, 4, '1', '2', '3 ');
Console. log (demoArray2); // => ['A', 'B', 'C', 'D']
Console. log (demoArray); // => ['1', '2', '3', 'A', 'B', 'C', 'D ', 'E']

4.16 toString (): concatenates all elements in the array with a comma (,) into a string.
Parameter: None

Return value:

All elements in the {String} array are concatenated into a String with a comma (,) and returned. It is the same as calling the join () method without parameters.

Example:

The code is as follows:


[1, 2, 3, 4, 5]. toString (); // => '1, 2, 3, 4, 5'
['A', 'B', 'C', 'D', 'E']. toString (); // => 'A, B, c, d, e'

4.17 unshift (): insert an element in the array header
Parameters:

① Value1, value2...... valueN: add any number of values to the array header.

Return value:

New length of {int} array

Example:

The code is as follows:


Var demoArray = [];
DemoArray. unshift ('A'); // => demoArray: ['A']
DemoArray. unshift ('B'); // => demoArray: ['B', 'A']
DemoArray. unshift ('C'); // => demoArray: ['C', 'B', 'A']
DemoArray. unshift ('D'); // => demoArray: ['D', 'C', 'B', 'A']
DemoArray. unshift ('e'); // => demoArray: ['e', 'D', 'C', 'B', 'A']

5. static method

5.1 Array. isArray (): determines whether the object is an Array.
Parameters:

① Value {Object}: any Object

Return value:

{Boolean} returns the judgment result. If it is true, it indicates that the object is an array; if it is false, it indicates that the object is not an array.

Example:

The code is as follows:


Array. isArray ([]); // => true
Array. isArray (['A', 'B', 'C']); // => true
Array. isArray ('A'); // => false
Array. isArray ('[1, 2, 3]'); // => false

6. Actual operation

6.1 index
Note: each element has a position in the array, which is represented by numbers and is called an index. The index starts from 0, that is, the index of the first element is 0, and the index of the second element is 1;

When an index does not exist in an array is obtained, undefined is returned.

Example:

The code is as follows:


Var demoArray = ['A', 'B', 'C', 'D', 'E'];
DemoArray [0]; // => obtain the first element: 'A'
DemoArray [0] = 1; // you can specify 1 as the first element.
Console. log (demoArray); // => demoArray: [1, 'B', 'C', 'D', 'E']
Console. log (demoArray [9]); // => undefined: When the retrieved index does not exist, undefined is returned.

6.2 for statement
Views: you can use the for statement to traverse arrays one by one.

Example:

The code is as follows:


Var demoArray = ['A', 'B', 'C', 'D', 'E'];
For (var I = 0, length = demoArray. length; I <length; I ++ ){
Console. log (demoArray [I]); // => outputs the elements in the array one by one
}

6.3 Replication
Note: The Array type is a reference type. When Array a is copied to Array B, elements of Array B are modified, and Array a is also modified.

Example:

The code is as follows:


Var demoArrayA = ['A', 'B', 'C', 'D', 'E'];
Var demoArrayB = demoArrayA; // assign array A to array B
DemoArrayB [0] = 1; // modify the elements of array B
Console. log (demoArrayA); // => [1, 'B', 'C', 'D', 'E']: the elements of array A have also changed.

6.4 deep replication
Note: The concat () method is used to return a new array. the element modification operation is performed on array B to prevent the replication of array.

Example:

The code is as follows:


Var demoArrayA = ['A', 'B', 'C', 'D', 'E'];
Var demoArrayB = demoArrayA. concat (); // use the concat () method to return a new array.
DemoArrayB [0] = 1; // modify the elements of array B
Console. log (demoArrayA); // => ['A', 'B', 'C', 'D', 'E']: the elements of array a have not changed.
Console. log (demoArrayB); // => [1, 'B', 'C', 'D', 'E']: Elements of array B have changed.

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.