JavaScript DHTML/Data Type/Array

Материал из Web эксперт
Перейти к: навигация, поиск

Array join()

   
    
<html>
<body>
<button onclick="var myA = new Array(10,11,12); 
                 alert(myA.join("---"));">Array: JOIN</button>
</body>
</html>



Array literal

   

<html>
  <head>
    <title>Array literal</title>
    <script type="text/javascript" >
var planets = ["mercury", "venus", "earth"];
document.write(planets);
    </script>
  </head>
  <body>
  </body>
</html>



Array.push

   
<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>



Array.splice

   
<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>



Concatenate Array with another Array

   
    
<html>
<body>
<button onclick="var myA = new Array(10,11,12); 
                 alert(myA.concat(13,14,15));">Array: CONCAT</button>
</body>
</html>



Convert Array to String

   
    
<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>



Create string array and reference its value

   

<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<p>");
  for (var i = 0; i < stringArray.length; i++) {
    document.write(i + 1 + ". " + stringArray[i] + "<br />");
  }
  document.write("</p>");
</script>
</body>
</html>



Join method with different parameters

   
<html>
  <head>
    <title>Array literal</title>
    <script type="text/javascript" >
var planets = ["A", "B", "C"];
var word = planets.join("");
document.write(word);
document.write("<BR>");
var list = planets.join();
document.write(list);
document.write("<BR>");
var sentence = planets.join(" then ");
document.write(sentence);

    </script>
  </head>
  <body>
  </body>
</html>



Loop through an array

   
<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]+"<BR>");
}
</SCRIPT>
</BODY>
</HTML>



Pop an element from Array

   
    
<html>
<body>
<button onclick="var myA = new Array(10,11,12); myA.pop(); alert(myA);">
Array: POP
</button>
</body>
</html>



Random sort an array of string

   
<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>



Reference array element by string index

   
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
var car= new Array()
car["a"]="A";
car["b"]="B";
car["c"]="C";
</SCRIPT>
</HEAD>
<BODY>
<H2>List of Cars:</H2>
<SCRIPT language="JavaScript">
 document.write(car["a"]+"<BR>");
 document.write(car["b"]+"<BR>");
 document.write(car["c"]);
</SCRIPT>
</BODY>
</HTML>



Reverse an Array

   
    
<html>
<body>
<button onclick="var myA = new Array(10,11,12); myA.reverse(); 
                 alert(myA);">Array: REVERSE</button>
</body>
</html>



shift an Array

   
    
<html>
<body>
<button onclick="var myA = new Array(10,11,12); 
                 myA.shift(); 
                 alert(myA);">
                 Array SHIFT
                 </button>
</body>
</html>



Sort an Array

   
    
<html>
<body>
<button onclick="var myA = new Array(1,15,13,11,2,4,16); myA.sort(); alert(myA);">
Array sort
</button>
</body>
</html>



Sort number array ascending

   
<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>



Splice an Array

   
    
<html>
<body>
<button onclick="var myA = new Array(10,11,12,13,14,15); alert(myA.splice(1,3));">
Array splice
</button>
</body>
</html>



To Array Source

   
    
<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>



Unshift array

   
    
<html>
<body>
<button onclick="var myA = new Array(10,11,12); myA.unshift(14); alert(myA);">
unshift
</button>
</body>
</html>



Use alert to output an array

   
<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>



Use default sort() method to sort an array

   

<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>



Use string as the index and use for loop to loop through it

   
<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("<BR>");
}

    </script>
  </head>
  <body>
  </body>
</html>



Use variable as the array index

   
<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>