ECMAScript array Arrays

Source: Internet
Author: User

ECMAScript Array

Self-summing up the ECMA array bar, the total feeling of learning the mess, what all know, and as if nothing knows!

The most basic is the creation of the array, add, delete ... Code Bar

1 <script type= "Text/javascript" > 6varOnUnload =function(){ 7 alert ("Are you sure you want to leave this interface?") "); 8 } 9 10varary =Newarray{"AA", "BB", "CC"};11 for(AAAinchary) {document.write (aaa+ "---");document.write ("");14 }15 16 17varA= {};18alert (A);</script>22 <script>25varary =NewArray (3);Ary[0] = "1";ARY[1] = "1";ARY[2] = "1";ARY[3] = "1";ARY[4] = "1";//Array (3) length is 3 instead of upper limit, if more than three, then default to Arrayl inside push in31//alert (ary);32 33 34varNewa =NewArray (1,2,2);/////cannot use var a = new Array (,,,);35 36//alert (newa.length);37//////////////////////38 39vars =NewArray ("AA", "a", "AAA");S[2] = "SFSDF";41//alert (s);S[4] = "Four";44 45//alert (s);//new s[4]S.length = 2;48//alert (s[4]);//Undefined length property is not a read-only property, equivalent to intercept length bitS.unshift ("add");//add data to the No. 0 bit of the arrayWuyi S.push ("push");//Add data to the end of the arrayS.splice ("Splice", 5);//add data to the nth position of an array71vararr =NewArray (3)Arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" 75 76varARR2 =NewArray (3)Arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" 80 81varMM =NewArray (2);Mm[0] = "00";MM[1] = "02";MM[2] = "01";MM[3] = "03";86alert (mm);87varAA =NewArray (100);88document.write (Aa.concat (mm))&LT;/SCRIPT&GT;96 97

The Concat () method is used to concatenate two or more arrays. This method returns a new array without changing the original array.

var New Array (2); mm[0] = "xx"; mm[1] = ""; mm[2] = ""varnew Array (10< c8>); document.write (Aa.concat (mm))
View Code

detecting arrays

instanceof

typeof

Constructor

Alert (typeof(aa));  // Object instanceof Array);  // true  //  true

Conversion method

All objects have tolocalestring (), toString (), and valueof () methods,

Tostring

The ToString method in which the array is called returns a comma-separated string of strings that are stitched together by the string of each value in the array, and the code

 <script type= "Text/javascript" >var  arr = new  Array (1,2,3 1 typeof  (arr)); //  object  alert (arr instanceof  Array); //  true  alert (arr instanceof  String); // false before conversion is not a string  //  toString  alert (typeof  (arr.tostring ())  ); // string  </script> 

ValueOf

Compared to ToString, ValueOf () is still the array, code bar

<script type= "Text/javascript" >vararr =NewArray (All-in-); alert (arr); alert (arr[1]); Alert (typeof(arr));//ObjectAlert (arrinstanceofArray);//trueAlert (arrinstanceofString);//false//ToString () alert (typeof(Arr.tostring ()));//String//ValueOf () alert (typeof(Arr.valueof ()));//object unchanged after conversionAlert (arrinstanceofArray);//trueAlert (arrinstanceofString);//false</script>

As shown above, the ToString and valueof () methods are called to return as strings, and the string representation of each value is stitched together into a string, separated by commas.

Also, calling the alert () method requires the string parameter to be received, so the ToString () method is called in the background, resulting in the same result as ToString.

Tolocalesting

When using the Tolocalesting method, the result of the call is the same as the first two, but it is not always the case. When the toLocaleString () method of an array is called, he creates a comma-separated string of array values. And the first two differences are that this time in order to get the value of each item, the tolocalestring () method of each item is called, not the ToString method ! That's the word, the code.

varstr ={toString:function(){return"Normal";},tolocalestring:function(){return"Locale";} };varSTR1 ={toString:function(){return"Normal01";},tolocalestring:function(){return"Locale01";} };varSTRs =[STR,STR1];//alert (STRs); Normal,normal01Alert ("typeof (STRs):" +typeof(STRs));//ObjectAlert ("typeof (Strs.tostring ()):" +typeof(Strs.tostring ()));//stringAlert ("typeof (Strs.tolocalestring ()):" +typeof(Strs.tolocalestring ()));//StringAlert ("Strs.tolocalestring ():" +strs.tolocalestring ());//LOCALE,LOCAL01 calls locale instead of default ToString

Creates two objects str1 str2, adds methods ToString and toLocaleString for each object, and returns a different value for two methods to detect whether the default ToString is called.

The STRs array contains these two types of objects, alert results:

Alert (STRs) Benson, returns and displays the same result as the call to ToString : normal,normal01

Alert whose type is Object

Alert displays the type of the string that called the ToString transformation, returning a string

When alert displays the type of the string to which the tolocalestring is converted, it returns string

Alert displays a string that invokes the tolocalestring conversion, resulting in a string from the toLocaleString method inside the two objects: LOCALE,LOCALE01, The reason is that the tolocalestring () of each item of the array is called

Join

The array inherits the toLocaleString, ToString, valueof methods, which, by default, return array items as comma-separated strings. If you use the Join method, you can use a different delimiter to build the string!

The Join method accepts only one parameter , which is a string used as a delimiter, and returns a string containing all the array items!

var ww = ["W", "X", "Max"];d ocument.write (ww.join ('---'));   W---x---90

When using the Join method, the output of the ToString () method is reproduced, and in the case of passing '---', an array value consisting of a string separated by a secondary delimiter is obtained.

Also, if you do not assign a value, or pass in undefined, then take the default!

Stack method

As is known to all, the stack is LIFO (LIFO), and ECMA provides a way to make arrays resemble other data structures--stacks!

is the stack, then there will be a stack and out of the stack, the ECMA provides both methods of push () and pop!

Push can accept any number of arguments and add them to the length of the stack one by one, and the returned lengths are len+length+1

Pop, the last item is removed from the end of the array, the length value of the array is reduced, and the removed item is returned.

var ww = ["W", "X", "Max"];d ocument.write (ww.join ('---') + "<br/>");    var New Array (); var ad = S.push ("ww", "xx");  // back to position 2  = S.push ("1990");   // return to position 3alert (AD);

var po = s.pop (); Get last item by default
Alert (PO); 1990 //Returns the value of the last item of the obtained array
alert (s.length);

Queue Method

Queue is not strange, first in and out (FIFO)

。。。。。。。。

Short hemp, work, go back and write ~~~~~

ECMAScript array Arrays

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.