An array of javascript

Source: Internet
Author: User

First, define the array.

There are two ways to define an array:

1, var arr1 = []; Define an empty array

2, var = [arr2, "str1", "str2"]; Defines an array of 5 elements.

3, var arr3 = new Array (3); Define an empty array

4, var arr4 = new Array ("str1", "str2"); Defines an array with a specified length of 5.

Second, the reading and writing of array elements.

ARR[0]; Reads the first array element

Arr[0] = "STR1"; Changes the value of the first element of the array.

Third, sparse array.

A sparse array represents an array of non-contiguous indexes starting at 0. Usually the length of the array represents the number of elements in the element, and if the array is sparse, the length property value will be greater than the number of elements.

The in operator is used to detect whether an element exists in a position, and note that undefined is also present.

such as: var a1 = [,,];

var a2 = new Array (3);

0 in A1; True, because a[0] has undefined elements

0 in A2; FALSE,A2 no element at index 0

Four, array length

The length property is used to flag array lengths

such as: var arr = [1,2,3,4,5];

Arr.length; 5 arr Array has 5 elements

V. Additions and deletions of array elements

Push://Add an element at the end of the array

var arr = [n/a];

Arr.push (4,5); Arr becomes [1,2,3,4,5]

Delete://Remove elements from an array location

var arr = [[+]

Delete Arr[1]//arr changed to [1,,3]

1 in Arr//false

VI. Traversal of arrays

The traversal of an array is usually accomplished by using the For statement

var arr = [1,2,3,4,5];

for (var i = 0.i<arr.length;i++) {

if (!a[i]) continue; Skipping null,undefined and non-existent elements

}

Seven, multidimensional arrays

A multidimensional array is an element or an array in an array.

such as: var arr = [[1,2,3],[,4,5,6]];

ARR[1][1]; 5

Eight, array method

1. Join () is used to convert all elements in an array into strings and join together, as well as to customize the connection character

var arr = [n/a];

Arr.join (); = "the"

Arr.join ("= ="); = "1==2==3";

2, reverse () to reverse the order of the array elements

var arr = [n/a];

Arr.reverse (); Arr array becomes [3,2,1]

3, sort (); Used to sort elements inside an array. You can pass in a function as a sort, or in alphabetical order if it is empty. undifined elements to the end of the line

var arr = [n/a];

A.sort (function (b) {

return a-B; Sort the standard negative number 0 positive, the comparison results first return the small one

}); The value of the ARR array is [b-a] and the result is [3,2,1] if the second condition becomes a.

4, concat ()//used to combine a new array, return a new array

var arr = [[+]

Arrnew = Arr.concat (4,5)//arrnew array is [1,2,3,4,5]

Arrnew1 = Arr.concat ([4,5],[6,7]); The arrnew1 array is [1,2,3,4,5,6,7]

5, slice ()///used to return an array of elements of the specified interval of an array, if a parameter is entered, it is the array from this parameter to the end. Two parameters is that the first parameter is the starting position, and the second parameter is the number.

var arr = [1,2,3,4,5];

var arr1 = Arr.slice (2); [3,4,5]

var arr2 = Arr.slice (1,3); [2,3]

6, splice () delete or add elements. Will change the original array itself, equivalent to a reference in C # (ref), the original array is an array of deleted elements, and the return value is an array of the remaining elements.

var arr = [1,2,3,4,5];

var arr1 = Arr.splice (1,3); ARR is [2,3,4], the returned array arr1 is [1,5]

var arr2 = [1,2,3,4,5];

var arr3 = Arr2.splice (2,0, ' A ', ' B '); Delete from 2nd bit, delete two elements, then insert ' a ', ' B ' from that position, arr2 [], because no element is removed, arr3[1,2, ' A ', ' B ', 3,4,5]

7. Push () and pop () add or remove an element at the end of the array, and when added, returns the last element added, when deleted. The return value is the deleted element.

The push () function adds an element to the tail of the array.

The pop () function deletes the last element of the array.

var arr = [[+]

Arr.push (4); Arr for [1,2,3,4]

var = [ARR1]

Arr.pop (); ARR1 for [all]

8, Unshift () and Shift ()

Shift (), unshift (), and push (), pop () are simply operations on the array head rather than the tail.

Shift () removes an element from the head of the array and returns a value of the deleted element.

Unshift () Adds an element to the head of the array, returning the group as the last element added.

var arr = [n/a];

var a = Arr.shift (); Arr becomes [2,3] a for 1

var = [arr1];

var B = Arr1.unshift ([4,5]); ARR1 becomes [4,51,2,3],b 4 returns the last one added, add 5 and add 4

9. ToString () and tolocalestring () convert the array to a string

var arr = [[+]

Arr.tostring (); The generation of "+" is the same as a join () that does not use any parameters.

Two, array method in ECMAScript

1, foreach () foreach () iterates through the array, invoking the specified function for each element.

      var arr = [1, 2, 3, 4, 5];        var sum = 0;        Arr.foreach (function (value) {            sum = sum + value;        });        document.write (sum); Sum ends up being 15

2. The map () method passes each element of the called array to the specified function and returns an array.

      var arr = [1, 2, 3, 4, 5];        var arr1 = Arr.map (function (value) {            return value + 1;        });        document.write (Arr1.join ()); ARR1 for [2,3,4,5,6]

3, filter () filters (), the returned element is a subset of the call array, filtering out the elements that do not meet the criteria.

        var arr = [1, 2, 3, 4, 5, 6];        var arr1 = arr.filter (function (i) {return I% 2 = = 0});        document.write (Arr1.join ()); ARR1 for [2,4,6]

4, every () and some ()

Every () returns true if and only if all elements in the array call the decision function to return true. Stops the traversal the first time it returns false.

Some () returns True when an element in the array called the decision function returns TRUE. Stops the traversal the first time it returns true.

        var arr = [1, 2, 3, 4, 5, 6];        var a = Arr.every (function (x) {return x > 3;});        var B = arr.some (function (y) {return y > 3;});        document.write (the value of "a" is: "+ a);  The value of a is false,a not all elements are greater than 3        document.write ("B's value is:" + b); The value of B is that there is an element greater than 3 in True,b

5. Reduce () and reduceright ()

Reduce () combines the elements in the array with the specified function to generate a single value, the first parameter is a simplified operation function, and the second argument is the initial value passed to the function. The final result is that the initial value is calculated once again by the combined function and the final result. The second parameter, the initial value, can be omitted, and the initial value is omitted to be calculated directly from the first element.

        var arr = [1, 2, 3, 4, 5, 6];        var count = arr.reduce (function (x, y) {return x + y;},0);        document.write (count);

Reduceright (); The only difference from reduce () is that it selects elements from right to left for calculation.

6, IndexOf () and Lastinsexof ()

IndexOf () indexOf () returns the index of the first element found at the beginning of the tail.

LastIndexOf () lastIndexOf () returns an element in reverse, returning the index of the first element found.

        var arr = [1, 2, 3, 2, 1];        var i = Arr.indexof (2);  Search from beginning to finish, first meeting 2 is arr[1], so return 1        var j = arr.lastindexof (2); Search from tail to head, first meeting 2 is arr[3], so return 3        document.write (i + "<br/>");        document.write (j);

7, Array.isarray (); Determines whether an object is an array object

        var arr = [1, 2, 3];        var str = "str1";        document.write (Array.isarray (arr)); Returns True if Arr is an        array object  

An array of javascript

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.