Js_Array array object

Source: Internet
Author: User
The Array object in js is a dynamic Array and does not need to specify the size in advance. [Javascript] 1. varnamesnewArray (); 2. names [0] & quot; tomy & quot; 3. names [1] & quot; jerry & quot; 4. names [2] & quot; lily & quot; 5.for( Array object in vari0; js is a dynamic Array and does not need to specify the size in advance.
[Javascript]
1. var names = new Array ();
2. names [0] = "tomy ";
3. names [1] = "jerry ";
4. names [2] = "lily ";
5. for (var I = 0; I 6. alert (names [I]);
7 .}
Write a function to calculate the maximum value of an array.
[Javascript]
1. // define a function
2. function getMax (arr ){
3. var max = arr [0]; // returns the first number in the array to max.
4. for (var I = 0; i5. if (arr [I]> max) {// if the current value is greater than max
6. max = arr [I]; // then, max is the current value.
7 .}
8 .}
9. return max; // return the value of max.
10 .}
11. // define an array
12. var num = new Array ();
13. num [0] = 3;
14. num [1] = 1;
15. num [2] = 5;
16. num [3] = 2;
17. // calculate the maximum value
18. alert (getMax (num ));
 
Reverse the order of the elements of a string array, thought: swap the I and the length-i-1 (js has a reverse function reverse, but here we write one by ourselves)
[Javascript]
1. // borrow the num Array
2. // output the original
3. document. write (num );
4. // define the function www.2cto.com
5. function myreverse (arr ){
6. for (var I = 0; i7. var temp = arr [I];
8. arr [I] = arr [arr. length-i-1];
9. arr [arr. length-i-1] = temp;
10 .}
11 .}
12. // Process
13. myreverse (num );
14. // output after processing
15. document. write (num );
 
Output A String Array as a | split string. (Js has a join function. num. join ("|") concatenates an array with a separator into a string, which we can write here .)
[Javascript]
1. // define a function
2. function myjoin (arr ){
3. if (arr. length <= 0 ){
4. return "";
5 .}
6. var s = arr [0];
7. for (var I = 1; i8. s = s + "|" + arr [I];
9 .}
10. return s;
11 .}
12. // define an array
13. var name = new Array ();
14. name [0] = "tomy ";
15. name [1] = "mary ";
16. name [2] = "peter ";
17. // call the Function
18. alert (myjoin (name ));
 
From Click Here

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.