JS array-first step, JS array-No

Source: Internet
Author: User
Tags array definition

JS array-first step, JS array-No

Array definition:

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

The length attribute of the array:

1. length: You can get the length of the array and set the length of the array.

2. Different from other languages, the js array length is dynamically changed. js Arrays can store any type of data, but it is best to store only one type of data.

 

1. The method push () adds an element to the array, and the returned value is the length of the new array.

<Script> var arr = [,]; var res = arr. push (3); alert (res); // The returned value is 5 in length. </script>

2. the pop () method removes an element from the end of the array. The returned value is the removed element.
<Script> var arr = [,]; var res = arr. pop (); alert (res); // The returned value is the removed Element 9 </script>
3. shift () removes an element from the array header. The returned value is the removed element.

<Script> var arr = [,]; var res = arr. shift (); alert (res); // return value: 12 elements removed </script>

4. The unshift () method inserts an element from the array header, and the returned value is the length of the new array.

<Script> var arr = [,]; var res = arr. unshift (3); alert (res); // return the array length of 5 </script>

5. The slice () method is used to intercept arrays. the return value is the truncated value (left closed and right open)

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


6. Method splice () is used to add elements to the array and delete elements from the array. The returned value is the deleted element.

Var arr = [12, 5, 8, 9]; var res = arr. splice (); // Delete the three elements alert (res) starting from the value of 1; // The elements returned are deleted: 5, 8, 9 alert (arr ); // returns 12 visible results. The splice method operates the array itself.

Var arr = [12, 5, 8, 9]; var res = arr. splice (,); // delete three elements starting from the value with the subscript of 1, and then add two elements, alert (res ); // The returned value is the deleted elements: 5, 8, 9 alert (arr); // the return value is 12, 44, 55.

Var arr = [12, 5, 8, 9]; var res = arr. splice (,); // Delete 0 elements from the value of subscript 1 (do not delete the element), and then add 2 elements from the position of subscript 1, 44,55 alert (res); // The returned value is the deleted element: Empty alert (arr); // return 12, 44, 55, 8, 9

7. The concat () method is used to connect two or more arrays. The returned 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
Connect multiple arrays

 

Var arr1 = [,]; var arr2 = []; var arr3 = ['A', 'B']; var res = arr1.concat (arr2, arr3 ); alert (res); // print 12, 5, A, Balert (arr1); // print 12, 5, it can be seen that the concat method does not operate the array itself alert (arr3); // print A, B


8. join (separator): concatenate each element in the array using a separator and return the result after the connection (string form)

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

Supplement:

// Use split of a string in js: used to split the string into arrays var str = '12*5*8 * 9'; var res = str. split ('*'); // use the character * to separate alert (res );

9. the sort () method sorts array elements in ascending order (from small to large). By default, elements are sorted in ascending order of the ASCII alphabet (note: the numbers are not sorted by the number size)
Var arr = [12, 5, 8, 9, 10, 1]; var res = arr. sort (arr); alert (res); // print the result, which is not what we think of, (arr); // print, 12, 5, 8, 9. You can see that the sort method operates the array itself.
For clarity Sort sorting principle

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 Sequence

1) numbers 0 ~ 9 is smaller than a letter. For example, "7" <"F ";

2) The number 0 is smaller than the number 9, and increases sequentially from 0 to 9. For example, "3" <"8"

3) the letter A is smaller than the letter Z and increases sequentially from A to Z. For example, "A" <"Z", "a" <"z"

4) uppercase letters are smaller than lowercase letters. For example, "A" <"a", "D" <"".

If you want to use sortCompare the number size, We needCustomize a "comparison function"Send it as a parameter to sort. For example

Function compare (n1, n2) {// UDF for comparison 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); // sort the numbers in positive order using a custom function alert (res); // print

// Simple writing function compare (n1, n2) {return n1-n2; // sort from small to large} var arr = [,]; var res = arr. sort (compare); // sort the numbers in positive order using a custom function alert (res); // print

If you want to implement Sort from big to small, Only need return n2-n1

Function compare (n1, n2) {return n2-n1; // sort from big to small}

10. The revers method is used to reverse sort the Array (the sorting principle is to reverse the position of the array element). Simply put, it is to flip all the elements in the array.

Var arr = [12, 5, 8, 9, 10, 2]; var res = arr. reverse (); alert (res); // print 2.10, 2.10, alert (arr); // print, visible reverse Method Operation Array itself
// String var arr = ['hangsan', 'lisi', 'hangzhou', 'zhaoliu ']; var res = arr. reverse (); alert (res); // print Zhaoliu. wangwu, Lisi, Zhangsan


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.