Introduction to JavaScript Array objects

Source: Internet
Author: User
Tags add empty end header string sort tostring javascript array

Array arrays
1. Introduce

An array is an ordered collection of values. Each value is called an element, and each element has a position in the array, represented by a number, called an index. JavaScript arrays are untyped: array elements can be of any type, and different elements of the same array may have different types. --"JavaScript Authority Guide (Sixth edition)"
2. Define

var names = new Array ("John", "Dick", "Harry");
Or
var names = ["John", "Dick", "Harry"];

3. Property

Length: Represents the element lengths within an array.
4. Instance method

Common methods:

1) unshift (): Inserts an element into the array header

2) Shift (): Removes and returns the first element of an array

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

4 pop (): Remove and return the last element of the array
4.1 concat (): Connect elements to an array. Does not modify the original array, returns the new array

Parameters:

①value1,value2.....valuen: Any number of values

return value:

{array} A new array containing the original array and the newly added elements.

Example:

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 (): sequentially iterate through the elements to determine whether each element is true

Parameters:

①function (value,index,self) {}: Each element uses this function to determine whether it is true and ends the traversal immediately when it is judged to be false.

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value:

{Boolean}: Returns true only if each element is true; False if one is false.

Example:

var demoarray = [1, 2, 3];
var rs = demoarray.every (function (value, index, self) {
return value > 0;
});
Console.log (RS); => true


4.3 Filter (): Iterates through the elements and returns a new array containing the conditional elements.

Parameters:

①function (value,index,self) {}: Each element calls this function in turn, and returns a new array containing the conditional elements.

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value:

{array} A new array containing qualified elements

Example:

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 (): Iterates through the elements, executes the specified function, and no return value.

Parameters:

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

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value: None

Example:

var demoarray = [1, 2, 3];
Demoarray.foreach (function (value, index, self) {
Console.log (value); => output in turn: 1 2 3
});


4.5 indexOf (): Finds a matching element in an array. If no matching element exists, return-1. Use the "= =" operator when looking, so distinguish between 1 and ' 1 '

Parameters:

①value: The value to find in the array.

②start: The ordinal position at which to start the lookup, or 0 if omitted.

return value:

{INT}: Returns the ordinal number of the first matching value in the array, if it does not exist, returns-1

Example:

[' 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: using the ' = = = ' Matching method


4.6 Join (): concatenation of all elements in an array into a string by a delimiter.

Parameters:

①sparator {String}: The delimiter between the elements, if omitted, by default because of the English comma ', ' delimited.

return value:

{string}: A string that is spliced together with sparator as delimiters.

Example:

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


4.7 LastIndexOf: Reverse lookup of matching elements in an array. If no matching element exists, return-1. Use the "= =" operator when looking, so distinguish between 1 and ' 1 '

Parameters:

①value: The value to find in the array.

②start: The ordinal position at which to start looking and, if omitted, the last element.

return value:

{INT}: Starts from right to left to find the ordinal number of the first matching value in the array, if it does not exist, returns-1

Example:

[' 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: using the ' = = = ' Matching method


4.8 Map (): Iterate through and compute each element to return an array of calculated elements

Parameters:

①function (value,index,self) {}: Each element calls this function in turn, returning the calculated element

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value:

{array} A new array that contains even good elements

Example:

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


4.9 pop (): Removes and returns the last element of an array

Parameters: None

return value:

The last element of an array of {Object}; If the array is empty, return undefined

Example:

var demoarray = [' A ', ' B ', ' C '];
Demoarray.pop (); => C
Demoarray.pop (); => b
Demoarray.pop (); => A
Demoarray.pop (); => undefined


4.10 Push (): Add elements to the tail of the array

Parameters:

①value1,value2.....valuen: Add any number of values to the tail of the array

return value:

The new length of the {int} array

Example:

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 (): Reverses the order of the array elements.

Parameters: None

return value: None (reverse element order within the original array).

Example:

var demoarray = [' A ', ' B ', ' C ', ' d ', ' e '];
Demoarray.reverse ();
Console.log (Demoarray); => ["E", "D", "C", "B", "a"]


4.12 Shift (): Removes and returns the first element of an array

Parameters: None

return value:

{Object} is the first element of an array; If the array is empty, return undefined.

Example:

var demoarray = [' A ', ' B ', ' C '];
Demoarray.shift (); => A
Demoarray.shift (); => b
Demoarray.shift (); => C
Demoarray.shift (); => undefined


4.13 Slice (Startindex,endindex): Returns part of an array.

Parameters:

①startindex: The ordinal number at the beginning, or negative, which means that the end is calculated from the tail-1 represents the last element,-2 is the penultimate, and so on.

②endindex: At the end of the element after an ordinal number, not specified is the end. The intercepted element does not contain an element with an ordinal number at the end of the first element of the ordinal.

return value:

{array} A new array containing all of the elements from startindex to Endindex.

Example:

[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]: intercept from ordinal 1
[1, 2, 3, 4, 5, 6].slice (0, 4); => [1, 2, 3, 4]: element that intercepts ordinal 0 to ordinal 3 (the previous one of ordinal 4)
[1, 2, 3, 4, 5, 6].slice (-2); => [5, 6]: intercept the following 2 elements


4.14 Sort (Opt_orderfunc): Sorting by certain rules

Parameters:

①opt_orderfunc (V1,V2) {function}: Optional collation function. If omitted, will be sorted according to the letter of the elements from small to large.

V1: The elements in front of the calendar.

V2: The elements behind the diachronic.

Sorting rules:

Compares V1 and V2, returning a number to represent the V1 and V2 collation:

Less than 0:v1 is less than V2,V1 in front of v2.

equals 0:v1 equals v2,v1 in front of v2.

Greater than 0:V1 is greater than v2,v1 row behind V2.

return value: None (sort operations in the original array).

Example:

[1, 3, 5, 2, 4, one, 22].sort (); => [1, 11, 2, 22, 3, 4, 5]: Here all elements are converted to characters, 11 characters before 2

[1, 3, 5, 2, 4, one, 22].sort (function (v1, v2) {
return v1-v2;
}); => [1, 2, 3, 4, 5, 11, 22]: Small to large sort

[1, 3, 5, 2, 4, one, 22].sort (function (v1, v2) {
Return-(V1-V2); To reverse, you can convert from large to small
}); => [22, 11, 5, 4, 3, 2, 1]


4.15 splice (): Inserting, deleting array elements

Parameters:

①start {int}: Starting sequence number at which to begin inserting, deleting, or replacing.

②deletecount {int}: The number of elements to delete, starting at start.

③value1,value2. Valuen {Object}: An optional parameter that represents the element to be inserted, starting at the start of the insertion. If the ② parameter is not 0, then the delete operation is performed before the insert operation is performed.

return value:

{array} returns a new array containing the deleted elements. If the ② parameter is 0, it means that no element is removed and an empty array is returned.

Example:

1. Delete
var demoarray = [' A ', ' B ', ' C ', ' d ', ' e '];
var demoArray2 = demoarray.splice (0, 2); Deletes the 2 elements starting with the ordinal number from 0, and returns 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 '); ② parameter 0, returns an empty array
Console.log (DemoArray2); => []
Console.log (Demoarray); => [' 1 ', ' 2 ', ' 3 ', ' A ', ' B ', ' C ', ' d ', ' e ']

3. Delete and insert first
var demoarray = [' A ', ' B ', ' C ', ' d ', ' e '];
When the ② parameter is not 0, the deletion is performed first (delete the 4 elements starting with the ordinal number 0, return the array containing the deleted element), and then perform the insert operation
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 (): Concatenation of all elements in an array by an English comma ', ' into a string.

Parameters: None

return value:

{string} All elements in an array are spliced into a string by an English comma ', ' and returned. The same as calling the parameterless join () method.

Example:

[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 (): Inserts an element into the array header

Parameters:

①value1,value2.....valuen: Add any number of values to the array header

return value:

The new length of the {int} array

Example:

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 an object is an array

Parameters:

①value {Object}: any object

return value:

{Boolean} Returns the result of the judgment. When true, to represent an object as an array;

Example:

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

Description: Each element has a position in the array, represented by a number, called an index. The index is from 0, that is, the first element has an index of 0, the second element is 1, and so on;

Returns undefined when an index that does not exist for an array is fetched.

Example:

var demoarray = [' A ', ' B ', ' C ', ' d ', ' e '];
DEMOARRAY[0]; => gets the first element: ' A '
Demoarray[0] = 1; Set the first element to 1
Console.log (Demoarray); => demoarray:[1, ' B ', ' C ', ' d ', ' e ']
Console.log (demoarray[9]); => undefined: Returns undefined when the retrieved index does not exist


6.2 For statement

Description: You can iterate through the array by using the For statement

Example:

var demoarray = [' A ', ' B ', ' C ', ' d ', ' e '];
for (var i = 0, length = demoarray.length i < length; i++) {
Console.log (Demoarray[i]); => the elements in an array output-by-individual
}


6.3 Light Copy

Description: The array type is a reference type, and when array A is copied to array B, an element modification is made to arrays B, which also changes.

Example:

var Demoarraya = [' A ', ' B ', ' C ', ' d ', ' e '];
var demoarrayb = Demoarraya; Assigning an array A to array b
Demoarrayb[0] = 1; Modify the elements of array B
Console.log (Demoarraya); => [1, ' B ', ' C ', ' d ', ' e ']: Elements of array A have also changed


6.4 Deep Copy

Note: Use the concat () method to return the new array, prevent the occurrence of the shallow copy, the element modification operation of array B, and the no change.

Example:

var Demoarraya = [' A ', ' B ', ' C ', ' d ', ' e '];
var Demoarrayb = Demoarraya.concat (); Returns a new array using the Concat () method
Demoarrayb[0] = 1; Modify the elements of array B
Console.log (Demoarraya); => [' A ', ' B ', ' C ', ' d ', ' e ']: Elements of array A are not changed
Console.log (DEMOARRAYB); => [1, ' B ', ' C ', ' d ', ' e ']: Elements of array B have changed



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.