JavaScript Array Object

Source: Internet
Author: User
Tags joins javascript array

Original: JavaScript Array object

Array Arrays 1. Introduction

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 in the same array may have different types. --"JavaScript authoritative Guide (sixth edition)"

2. Definition
var New Array ("Zhang San", "John Doe", "Harry"); // or var names = ["Zhang San", "John Doe", "Harry"];
3. Properties

Length: Indicates how long the element is in the array.

4. Example Methods

Common Methods :

1) unshift () : Insert element at the head of the array

2) shift () : Remove and return the first element of the 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 (): Joins the element into the array. Does not modify the original array to return the new arrays

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

4.2 Every (): iterates through the elements to determine if each element is true

Parameters:

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

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value:

{Boolean}: Returns true only if each element is true, and 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, returning a new array containing the qualifying elements.

Parameters:

①function (value,index,self) {}: Each element calls this function sequentially, returning a new array containing the qualifying elements.

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value:

{array} A new array containing the qualifying 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 returns no value.

Parameters:

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

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value: None

Example:

var demoarray = [1, 2, 3];d Emoarray.foreach (function (value, index, self) {    console.log (value);//= = 1  2< C6/>3});

4.5 indexOf (): Finds the matching element in the array. If no matching element exists, 1 is returned. the "= = =" Operator is used when looking for, so distinguish between 1 and ' 1 '

Parameters:

①value: The value to look up in the array.

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

return value:

{INT}: Returns the ordinal of the first match value in the array, or 1 if it does not exist

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

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

Parameters:

①sparator {String}: Delimiter between elements, if omitted, by default as comma ', ' delimited.

return value:

{string}: Each element is delimited by sparator, a string that is stitched together.

Example:

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

4.7 lastIndexOf: Finds matching elements in the array in reverse. If no matching element exists, 1 is returned. the "= = =" Operator is used when looking for, so distinguish between 1 and ' 1 '

Parameters:

①value: The value to look up in the array.

②start: The ordinal position at which to start the lookup, or, if omitted, a lookup from the last element.

return value:

{Int}: Finds the ordinal of the first matching value in the array from right to left, and returns 1 if it does not exist

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

4.8 Map (): Iterates and computes each element sequentially, returning an array of calculated elements

Parameters:

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

Value: An element of an array traversal

Index: Element ordinal

Self:array itself

return value:

{array} A new array containing even the 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 the array

Parameters: None

return value:

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

Example:

var demoarray = [' A ', ' B ', ' C '];d emoarray.pop (); = Cdemoarray.pop (); = Bdemoarray.pop (); = Ademoarray.pop (); = undefined

4.10 Push (): Adds an element to the end of the array

Parameters:

①value1,value2.....valuen: Any number of values added to the tail of the array

return value:

The new length of the {int} array

Example:

var demoarray = [' A ', ' B ', ' C '];d emoarray.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 (element order reversal within the original array).

Example:

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

Parameters: None

return value:

The first element of the {Object} array, and returns undefined if the array is empty.

Example:

var demoarray = [' A ', ' B ', ' C '];d emoarray.shift (); = Ademoarray.shift (); = Bdemoarray.shift (); = Cdemoarray.shift (); = undefined

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

Parameters:

①startindex: The ordinal at the beginning, or, if negative, the beginning of the tail, 1 for the last element, 2 for the second, and so on.

②endindex: The end of the element after an ordinal, not specified is the end. The intercepted element does not contain the element where the ordinal is located, ending with the previous element of the ordinal.

return value:

{array} A new array that contains all the elements from startindex to the previous element of 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]: Starting from the serial number 1 intercept [1, 2, 3, 4, 5, 6].slice (0, 4); = = [1, 2, 3, 4]: The elements of the intercept sequence number 0 to the ordinal 3 (ordinal 4 of the previous) [1, 2, 3, 4, 5, 6].slice (-2); = = [5, 6]: intercept the 2 elements behind

4.14 Sort (Opt_orderfunc): Sort by a certain rule

Parameters:

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

V1: The elements that lasted before.

V2: The elements behind the time elapsed.

Sorting rules:

Compare v1 and V2, and return a number to represent the collation of V1 and V2:

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

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

Greater than 0:V1 is greater than v2,v1 in the back of 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]: From small to large sort [1, 3, 5, 2, 4, one, 22].sort (function (v1, v2) {    

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}: Optional parameter that represents the element to insert, starting at start. If the ② parameter is not 0, then the delete operation is performed before the insert operation.

return value:

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

Example:

1. remove var Demoarray = [' A ', ' B ', ' C ', ' d ', ' e '];var demoArray2 = Demoarray.splice (0, 2); Removes 2 elements starting with the ordinal 0, returning 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, returning an empty array of console.log (DemoArray2); = = []console.log (Demoarray); = = [' 1 ', ' 2 ', ' 3 ', ' A ', ' B ', ' C ', ' d ', ' e ']//3. First remove and then insert var demoarray = [' A ', ' B ', ' C ', ' d ', ' E '];//when ② parameter is not 0, then perform the delete (delete 4 elements starting with 0, return an array containing the deleted elements), then perform the insert operation var demoArray2 = demoarray.splice (0, 4, ' 1 ', ' 2 ', ' 3 '); Console.log (DemoArray2); = = [' A ', ' B ', ' C ', ' d ']  

4.16 toString (): Concatenation all elements of an array into a string by a comma ', '.

Parameters: None

return value:

All elements in the {string} array are stitched to a string by a comma ', ', and returned. Same as calling the No 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 (): Inserting elements in the head of an array

Parameters:

①value1,value2.....valuen: Any number of values added to the array header

return value:

The new length of the {int} array

Example:

var demoarray = [];d emoarray.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}: Arbitrary object

return value:

{Boolean} Returns the result of the decision. When True, indicates that the object is an array;

Example:

Array.isarray ([]); = = Truearray.isarray ([' A ', ' B ', ' C ']); = = Truearray.isarray (' a '); = = Falsearray.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 calculated starting at 0, that is, the first element has an index of 0, the second element has an index of 1, and so on;

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

Example:

var demoarray = [' A ', ' B ', ' C ', ' d ', ' e '];d emoarray[0]; + = Gets the first element: ' a ' demoarray[0] = 1;  Set the first element to 1console.log (Demoarray); = = demoarray:[1, ' B ', ' C ', ' d ', ' E ']console.log (demoarray[9]); = = undefined: Returns undefined when the obtained index does not exist

6.2 For statement

Description: You can iterate through the array by 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]); = = Output The elements in an array individually}

6.3 Shallow Copy

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

Example:

var Demoarraya = [' A ', ' B ', ' C ', ' d ', ' e '];var demoarrayb = Demoarraya; Assign array A to array bdemoarrayb[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 Copy

Description: returns a new array using the Concat () method, prevents the occurrence of a shallow copy, modifies an element in array B, and does not change.

Example:

var Demoarraya = [' A ', ' B ', ' C ', ' d ', ' e '];var Demoarrayb = Demoarraya.concat (); Use the Concat () method to return the new array demoarrayb[0] = 1; Modify the elements of array B console.log (Demoarraya); = = [' A ', ' B ', ' C ', ' d ', ' e ']: the element of array A has not changed Console.log (DEMOARRAYB); = = [  1, ' B ', ' C ', ' d ', ' e ']: the element of array B has changed

6.5 Determine whether 2 arrays are equal

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

Example:

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

 

================================== Series article ==========================================

This article: 3.3 JavaScript Array Object

Web Development Road Series articles

JavaScript Array Object

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.