JavaScript Array Object detailed _javascript tips

Source: Internet
Author: User
Tags instance method shallow copy javascript array

This paper describes the array of JS arrays, the specific contents are as follows

Directory
1. Introduction: Describes the description, definition, and properties of an array object.

2. Instance methods: Describes the instance methods of the Array object: Concat, every, filter, ForEach, IndexOf, join, LastIndexOf, map, pop, push, reverse, shift, slice, Sort, splice, toString, Tounshift and so on.

3. Static method: Describes the static method of an Array object: Array.isarray ().

4. Actual operation: The example operation of the Array: index, for traversal, shallow copy, deep copy operations.

I. INTRODUCTION
1.1 Description

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)"

1.2 Definition Way

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

1.3 Properties

Length: Represents the element lengths within an array.

Two. 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

2.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 ']

2.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
 

2.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]

2.4 ForEach (): Iterate through the elements, executing the specified function; 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 sequence: 1 2 3
});

2.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].index Of (' 1 '); =>-1: using the ' = = = ' Matching method

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

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 '

2.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
 

2.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]
 

2.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

2.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 ']

2.11 Reverse (): Reverses the order of 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"]

2.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

2.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,
6].slice (4);//=> [5, 1, 2, 3, 4]: Intercept from ordinal 5
[6, 1, 1, 2, 3, 6].sli CE (0, 4); => [1, 2, 3, 4]: Intercept The elements of the ordinal 0 to ordinal 3 (the previous one of the ordinal 4)
[1, 2, 3, 4, 5, 6].slice ( -2);//=> [5, 6]: intercept the 2 elements behind
 

2.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 of the 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);//Reverse, can be converted to from large to small
}); => [22, 11, 5, 4, 3, 2, 1]
 

2.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 '); The ② parameter is 0, which returns an empty array
console.log (demoArray2);//=> []
console.log (Demoarray);//=> [' 1 ', ' 2 ', ' 3 ', ' A ', ' B ', ' C ' , ' d ', ' e ']
 
//3. First delete and then insert
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 elements), and then execute the insert operation
var demoArray2 = demoarray.splice (0, 4, ' 1 ', ' 2 ', ' 3 '); C14/>console.log (DemoArray2); => [' A ', ' B ', ' C ', ' d '] 
Console.log (Demoarray);//=> [' 1 ', ' 2 ', ' 3 ', ' A ', ' B ', ' C ', ' d ', ' e ']
 

2.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 '

2.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 '

Three. static method
3.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

Four. Actual operation
4.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

4.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));//=> elements in array by output
}

4.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; Assign the array A to the array b
demoarrayb[0] = 1;//The Elements of Group B are modified
console.log (Demoarraya);//=> [1, ' B ', ' C ', ' d ', ' e ']: element of array A The element has also changed
 

4.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 (); Using the Concat () method, return the new array
demoarrayb[0] = 1;//The Elements of array B are modified
console.log (Demoarraya);//=> [' A ', ' B ', ' C ', ' d ', ' E ']: element of array A has not changed
Console.log (DEMOARRAYB);//=> [1, ' B ', ' C ', ' d ', ' e ']: Elements of array B have changed

4.5 to determine whether 2 arrays are equal

Description: Array arrays are reference types, so even if []===[] returns FALSE, the string returned by the array ToString () method can be judged to be equal.

Example:

Console.log ([]===[]); => false
Console.log ([' A ', ' b '] = = = [' A ', ' B ']);//=> false
Console.log ([' A ', ' B '].tostring () = = = [' A '], ' B '].tostring ()); True

This is the whole story, and I want to help you learn JavaScript array objects.

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.