JS Array-First foot

Source: Internet
Author: User
Tags array definition

Array definition:

var arr = [12,5,8,9];var arr = new Array (12,5,8,9);

The length property of the array:

1. length allows you to get the lengths of the array and the length of the array.

2. Unlike other languages, the length of the JS array is dynamically changed, and the JS array can hold any type of data, but it is best to store only one type of data

1. Method push () appends an element to the array, returning the value to the length of the new array.

<script>    var arr = [12,5,8,9];    var res = Arr.push (3);    Alert (res); The return value is the length of the array 5</script>

2. MethodsPop() removes an element from the end of the array and returns the element that is removed
<script>    var arr = [12,5,8,9];    var res = Arr.pop ();    Alert (res); The return value is the removed element 9</script>
3. MethodsShift() removes an element from the head of the array and returns the element that is removed

<script>    var arr = [12,5,8,9];    var res = Arr.shift ();    Alert (res); The return value is the removed element 12</script>

4. Method Unshift () Inserts an element from the head of the array, returning the value to the length of the new array

<script>    var arr = [12,5,8,9];    var res = Arr.unshift (3);    Alert (res); Returns the length of the array 5</script>

5. Method Slice () is used to intercept the array, the return value is the Intercept value (left closed right open interval)

<script>    var arr = [12,5,8,9];    Arr.slice (1,3) means: intercept subscript (greater than or equal to 1, less than 3) of the value    var res = arr.slice (1,3);    Alert (res); The return value is 5,8    alert (arr);//return 12,5,8,9 visible slice method does not manipulate the array itself </script>


6. Method Splice () is used to add elements to the array and also to remove elements from the array; The returned value is the deleted element

var arr = [12,5,8,9];var res = Arr.splice (1,3);//start with a value of subscript 1 and delete 3 element alert (res); The returned value is the deleted element: 5,8,9alert (arr); Returns a 12 visible splice method that will manipulate the array itself


var arr = [12,5,8,9];var res = Arr.splice (1,0,44,55);//start with a value of subscript 1, delete 0 elements (without deleting the element), and then add 2 elements, 44,55alert (res), from the position of the subscript 1; The return value is the deleted element: an empty alert (arr); Back to 12,44,55,8,9

7. Methodsconcat() to connect two or more arrays,The return value is a new array
var arr1 = [12,5,8,9];var arr2 = [33,44];var res = Arr1.concat (ARR2); alert (res); 12,5,8,9,33,44
concatenate multiple arrays

var arr1 = [12,5,8,9];var arr2 = [33,44];var arr3 =[' A ', ' B '];var res = Arr1.concat (ARR2,ARR3); alert (res); Print out 12,5,8,9,33,44,a,balert (arr1);//print 12,5,8,9, visible concat method does not manipulate the array itself alert (ARR3);//print A, b


8. Method Join (delimiter): Joins each element in the array with a delimiter, returning the concatenated result (as a string)  

var arr1 = [12,5,8,9];var res = arr1.join (' * '); alert (res); 12*5*8*9alert (ARR1);//print 12,5,8,9, the Join method does not manipulate the array itself

Add:

The string in JS has a split usage: used to split the string into an array of    var str= ' 12*5*8*9 ';    var res = str.split (' * ');//Use character * to split     alert (RES);

9. MethodsSort() Positive ordering of array elements(from small to large), the default is to pressASCIIorder the size of the alphabet (note: the numbers are not sorted by the size of the numbers)
var arr = [12,5,8,9,10,1];var res = Arr.sort (arr); alert (res); Printing results 1,10,12,5,8,9, not our imagined 1,5,8,9,10,12alert (arr);//print 1,10,12,5,8,9, visible the Sort method manipulating the array itself
in order to understand clearly sorting principle of sort

var arr = [12,5, ' a ', 8, ' a ', ' D ', 9,10,1, ' C '];var res = Arr.sort (arr); alert (res); 1,10,12,5,8,9,a,d,a,c

ASCII Table Size Order

1) The number 0~9 is smaller than the letter. such as "7" < "F";

2) The number 0 is smaller than the number 9 and increments by 0 to 9. such as "3" < "8"

3) The letter A is smaller than the letter z and is incremented by A to Z order. such as "a" < "Z", "a" < "Z"

4) Uppercase letters are smaller than lowercase letters. such as "a" < "a", "D" < "a".  

If you want to compare numbers in size by sort, we need to customize a "comparison function" to pass to sort as a parameter. For example

function Compare (N1,N2) {//Custom comparison functions        if (n1<n2) {            return-1;        }        else if (n1>n2) {            return 1;        }        else{            return 0;}        } var arr = [12,5,8,9,10,1];var res = arr.sort (compare);//positive ordering of numbers by a custom function alert (res); Print 1,5,8,9,10,12

Simple notation       function Compare (n1,n2) {        return n1-n2;//from small to large sort    }    var arr = [12,5,8,9,10,1];    var res = Arr.sort (compare);//positive ordering of numbers by a custom Function    alert (res);//Print 1,5,8,9,10,12

if you want to implement a sort from large to small , you only need to return n2-n1

function Compare (N1,N2) {        return n2-n1;//from large to small}

10. Methodsreversused to reverse the sequence of arrays (the principle of sorting is to invert the position of the array elements), simply to flip the elements in the array.

var arr = [12,5,8,9,10,2];var res = Arr.reverse (); alert (res); Print 2.10,9,8,5,12alert (arr);//print 2.10,9,8,5,12 visible reverse method operation array itself
The string    var arr = [' Zhangsan ', ' Lisi ', ' Wangwu ', ' Zhaoliu '];    var res = Arr.reverse ();    Alert (res); Print Zhaoliu.wangwu,lisi,zhangsan


JS Array-First foot

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.