<script language= "JavaScript" > /* Because JavaScript is an untyped language, elements of an array can have arbitrary data types, and different elements of the same array can have different types, the element settings of an array can contain other arrays, so you can create a complex array. And at this point JavaScript is different from the rigorous object-oriented C++.c#,java as a scripting language. With greater flexibility. */ /* * in javascript1.1 and subsequent versions, the array is created with the constructor array () and operator new. You can create an array in JavaScript in the following three ways. */ 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 action, Float data is also count the group, in fact not what you think JavaScript will convert it to a string when you are using negative numbers, floating-point numbers, (or Boolean, objects, other values) Using the generated string as the property name of the object, rather than defining a new array element The example above is the fact that a property named: "1.23" was 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 tests also make it clear that we use integers as subscripts for arrays to actually add an element to the array. This uses the length of the array to reflect the subtleties of the JavaScript array. The //can truncate the length of an array by setting an array's Length property. 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]=") ; } //here to test our multidimensional array elements /* *javascript does not actually support multidimensional arrays * But we assign elements of one one-dimensional array to one one-dimensional array, This looks like a multidimensional array, but is actually a one-dimensional array, which is the same idea we have when we understand an array of C, but their implementation mechanism is different. */ 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; docum Ent.write ("<br/>c[i]=" +c[i)); } The document.write ("<br/>c element Join () method is followed by:" +c.join ()); //Array reverse () method C.reverse (); document.write ("<br/>c element joins in the reverse () method () The result is: "+c.join (" | ")); Testing of the concat () method 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 that starts at the second parameter specified by the Element but does not contain the element specified by the second argument. Splice () method The splice () method is a common method for inserting or deleting an array element. /* The first parameter of the splice function specifies the position of the element to be inserted or deleted in the array. The second parameter specifies the number of elements to be removed from the array After the second parameter, you can have as many parameters as you want, and they specify the elements that are 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); Arrays in JavaScript are similar to PHP when used as stacks This is interesting and more useful. The following is a small instance used as a stack /* The push method is to attach 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 /* The Unshift method adds one or more elements to the head of an array element, and then moves the existing element to the largest position in the subscript and has room , it returns the new length of the host family. 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, the ToString () method that we are familiar with in Java is left It ' s a piece of cake! document.write (C.tostring ()); In fact, the effect of the array's ToString () method and the no-parameter join () is exactly the same Ok,this ' s chapter for Array,that ' s all! </script> |