JavaScript array use method Rollup _javascript tips

Source: Internet
Author: User
Tags javascript array

<script language= "JavaScript" >//author: Dong ge//date:2008-1-11//Purpose: Practice the basic operation of arrays/* Because JavaScript is an untyped language, the element of an array Elements can have any type of data, different elements of the same array can have different types, and the element settings of an array can contain other arrays so that a complex array can be created. And at this point JavaScript is different from the rigorous object-oriented C++.c#,java as a scripting language. With greater flexibility. * * * in the javascript1.1 and subsequent versions, the array is created with the constructor array () and operator new, and the following three ways can be used to create an array in JavaScript. * * var a=new Array (); var b=new Array (5,4,3, "a", "test,string"); var c=new Array (20); a[1.23]= "Test"; document.write ("a[1.23]=" +a[1.23]); Believe that every time you learn JavaScript from a strongly typed programming language, you'll definitely be surprised by this operation,//float data is also count the group, and it's not really what you want//javascript when you're using negative numbers, floating-point numbers, (or Boolean, objects, other values), JavaScript converts it to a string///using the generated string as the object's property name, rather than defining a new array element//The above instance the fact is that a property named: "1.23" is created for a. document.write ("a.length=" +a.length); document.write ("b.length=" +b.length); document.write ("c.length=" +c.length); A[3]= "Test"; document.write ("<br>a[3]=" +a[3]); document.write ("<br>a.length=" +a.length); The above test is also very clear that we use integers as the subscript of an array to really add an element to the array,//the length of the arrays here to reflect the subtlety of the JavaScript array. By settingThe length property of the array can be truncated to the lengths of the array. a.length=3; if (a[3]==undefined) {document.write ("<br>" after A.length= "+a.length+", a[3]= "+a[3"); else {document.write (<br> after A.length= "+a.length+", a[3]= "+a[3"); ///Here to test our multidimensional array elements/* *javascript In fact does not support multidimensional arrays * But we assign elements of one one-dimensional array to one one-dimensional array, so it looks like you're implementing a multidimensional array, but in fact he's a one-dimensional array, which, when we understand the C-language array, The same idea, but their implementation mechanism is not the same. * * var g=new Array (3); G[3]=a; g[3][2]= "Test" document.write ("<br>g[3][2]=" +g[3][2)); Array join () method for (var i=0;i<20; i++) {c[i]=i; document.write ("<br>c[i]=" +c[i]); The document.write ("<br>c element Join () method is followed by:" +c.join ()); The reverse () method of the array c.reverse (); The result of the document.write ("<br>c" element after the reverse () method is again join () is: "+c.join (" | ")); The Concat () method tests var h=new Array (1,2,3); H= H.concat ([4,5]); However, the concat function does not recursively expand an array of elements. H=h.concat (6,7,[9,[10,20]]); document.write ("<br>h.length=" +h.length+ "<br>" +h); document.write ("h[8]=" +h[8]); Slice () method document.write ("<br>h.slice (4,5) =" +h.slice (4,5)); DocUment.write ("H.slice (5,9) =" +h.slice (5,9))//slice () method: The returned array contains the element specified by the first argument and the element starting with the second parameter specified// Element but does not contain the element specified by the second argument. The splice () method//splice () method is a common method for inserting or deleting an array element. /* Splice function The first parameter specifies the position of the element to be inserted or deleted in the array. The second parameter specifies that the number of elements to be removed from the array can have as many arguments after the second argument as they specify the element to be inserted from the position specified by the first parameter. The first element and subsequent elements, make the corresponding move. * * document.write ("<br>h.splice (8,1) after H is::" +h.splice (8,1)); document.write ("<br>h.splice" (8,0, ' A ', ' B ', ' Test ') after H is:: "+h.splice (8,0, ' A ', ' B ', ' Test ')); H.splice (7,0, ' A ', ' B ', ' Test '); document.write ("<br>h.splice" (7,0, ' A ', ' B ', ' Test ') after H is: "+h); It's interesting and useful to have arrays in JavaScript that are similar to PHP as stacks. The following is the small instance used as a stack/* Push method is to append one or more new elements to the end of the array, and then return the new length of the array. The pop deletes the last element of the array, sticks to the length of the array, and returns the value he deleted. * * var stack=new Array (); Stack.push (1,2); document.write ("<br>stack element is:" +stack); document.write ("<br>stack.length=" +stack.length); The result of the document.write ("<br>stack.pop () return is:" +stack.pop ()); document.write ("<br>stack.length=" +stack.length); The following is a small instance used as a queue/* Unshift method to add one or more elements to an array elementThe head of the element, and then move the existing elements to the highest position of the subscript has freed up space, it returns the main family of the new length. Method Shift is to delete and return the first element of the array, and then move all subsequent elements forward to fill the space left by the first element. * * var list=[]; List.unshift (6,2); document.write ("<br>list content is:" +list); The document.write ("<br>list shift method is:" +list.shift ()); In addition, we are familiar with the ToString () method in Java//it ' s a piece of cake! document.write (C.tostring ()); Plainly, the effect of the array's ToString () method and the parameterless join () is exactly the same//ok,this ' s chapter for Array,that ' s all! </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.