JavaScript DHTML/Data Type/Array
Содержание
- 1 Array join()
- 2 Array literal
- 3 Array.push
- 4 Array.splice
- 5 Concatenate Array with another Array
- 6 Convert Array to String
- 7 Create string array and reference its value
- 8 Join method with different parameters
- 9 Loop through an array
- 10 Pop an element from Array
- 11 Random sort an array of string
- 12 Reference array element by string index
- 13 List of Cars:
- 14 Reverse an Array
- 15 shift an Array
- 16 Sort an Array
- 17 Sort number array ascending
- 18 Splice an Array
- 19 To Array Source
- 20 Unshift array
- 21 Use alert to output an array
- 22 Use default sort() method to sort an array
- 23 Use string as the index and use for loop to loop through it
- 24 Use variable as the array index
Array join()
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12);
alert(myA.join("---"));">Array: JOIN</button>
</body> </html>
</source>
Array literal
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" >
var planets = ["mercury", "venus", "earth"]; document.write(planets);
</script> </head> <body> </body>
</html>
</source>
Array.push
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" > var planets = ["m", "v", "a"]; planets.push("m", "j"); document.write(planets); </script> </head> <body> </body>
</html>
</source>
Array.splice
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" > planets = ["q", "v", "e", "a", "j"]; planets.splice(2, 2); document.write(planets); </script> </head> <body> </body>
</html>
</source>
Concatenate Array with another Array
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12);
alert(myA.concat(13,14,15));">Array: CONCAT</button>
</body> </html>
</source>
Convert Array to String
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(0,1,2,3,4,5);
alert(myA.toString());">call Array TOSTRING</button>
<button onclick="var myDate = new Date(); alert(myDate.toString());"> call Date toString</button> </body> </html>
</source>
Create string array and reference its value
<source lang="html4strict">
<html> <head> </head> <body> <script type="text/javascript">
var stringArray = new Array(); stringArray[0] = "A"; stringArray[1] = "B"; stringArray[2] = "C"; stringArray[3] = "D"; stringArray[4] = "E"; stringArray[5] = "F"; stringArray[6] = "G";document.write("String value
");
for (var i = 0; i < stringArray.length; i++) {
document.write(i + 1 + ". " + stringArray[i] + "
");
}
document.write("
</script> </body> </html>
</source>
Join method with different parameters
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" >
var planets = ["A", "B", "C"];
var word = planets.join("");
document.write(word);
document.write("
");
var list = planets.join();
document.write(list);
document.write("
");
var sentence = planets.join(" then ");
document.write(sentence);
</script> </head> <body> </body>
</html>
</source>
Loop through an array
<source lang="html4strict">
<HTML> <HEAD> <SCRIPT language="JavaScript"> var parts= new Array() parts[0]="monitor"; parts[1]="motherboard"; parts[2]="chip"; parts[3]="hard drive"; </SCRIPT> </HEAD> <BODY> <SCRIPT language="JavaScript"> for (count=0;count<parts.length;count+=1) {
document.write(parts[count]+"
");
} </SCRIPT> </BODY> </HTML>
</source>
Pop an element from Array
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12); myA.pop(); alert(myA);"> Array: POP </button> </body> </html>
</source>
Random sort an array of string
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" >
function compare(a, b) {
if (Math.random() * 2 > 1) { return 1; } else { return -1; }
}
var planets = ["m", "a", "e", "m", "j"]; planets.sort(compare); document.write(planets);
</script> </head> <body> </body>
</html>
</source>
Reference array element by string index
<source lang="html4strict">
<HTML> <HEAD> <SCRIPT language="JavaScript"> var car= new Array() car["a"]="A"; car["b"]="B"; car["c"]="C"; </SCRIPT> </HEAD> <BODY>
List of Cars:
<SCRIPT language="JavaScript">
document.write(car["a"]+"
"); document.write(car["b"]+"
"); document.write(car["c"]);
</SCRIPT> </BODY> </HTML>
</source>
Reverse an Array
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12); myA.reverse();
alert(myA);">Array: REVERSE</button>
</body> </html>
</source>
shift an Array
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12);
myA.shift(); alert(myA);"> Array SHIFT </button>
</body> </html>
</source>
Sort an Array
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(1,15,13,11,2,4,16); myA.sort(); alert(myA);"> Array sort </button> </body> </html>
</source>
Sort number array ascending
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" >
function compare(a, b) {
return a - b;
} var gravities = [0.8, 0.1, 1, 0.3, 2.4]; gravities.sort(compare); document.write(gravities);
</script> </head> <body> </body>
</html>
</source>
Splice an Array
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12,13,14,15); alert(myA.splice(1,3));"> Array splice </button> </body> </html>
</source>
To Array Source
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12,13,14,15);
alert(myA.toSource());">Array TOSOURCE</button>
<button onclick="var myDate = new Date(); alert(myDate.toSource());"> Date toSource </button> </body> </html>
</source>
Unshift array
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12); myA.unshift(14); alert(myA);"> unshift </button> </body> </html>
</source>
Use alert to output an array
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="javascript"> var days_of_week = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); alert(days_of_week); </script> </head> <body> </body> </html>
</source>
Use default sort() method to sort an array
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" >
var planets = ["a", "v", "asdf", "fdsa", "a"]; planets.sort(); document.write(planets);
</script> </head> <body> </body>
</html>
</source>
Use string as the index and use for loop to loop through it
<source lang="html4strict">
<html>
<head> <title>Array literal</title> <script type="text/javascript" >
var planets = []; planets["inner"] = ["A", "B", "C"]; planets["outer"] = ["a", "b", "c"];
for (var i in planets) {
document.write(planets[i]); document.write("
");
}
</script> </head> <body> </body>
</html>
</source>
Use variable as the array index
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="javascript"> var days_of_week = new Array(7); days_of_week[0] = "Sunday"; days_of_week[1] = "Monday"; days_of_week[2] = "Tuesday"; days_of_week[3] = "Wednesday"; days_of_week[4] = "Thursday"; days_of_week[5] = "Friday"; days_of_week[6] = "Saturday"; var x = 2; alert(days_of_week[x]); </script> </head> <body> </body> </html>
</source>