An array of javascript

Source: Internet
Author: User
Tags array length

    • Defining arrays

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.

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.

1. Sparse arrays.

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

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

3. Addition and deletion 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

4. Iterating the array

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

}

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

6. Array methods

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 [All-in-one] if the second condition becomes b-a the result is [3,2,1]

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.

    • Array methods 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 + value;     });     document.write (sum);  //sum is finally

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;        });         // arr1 for [2,3,4,5,6]

An array of javascript

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.