Array loop, find:Control array : Two dimension array
<!--
Example File From "JavaScript and DHTML Cookbook"
Published by O'Reilly & Associates
Copyright 2003 Danny Goodman
-->
var myArray = ["Alice", "Fred", "Jean", "Steve"];
for (var i = 0; i < myArray.length ; i++) {
alert("Item " + i + " is:" + myArray[i] + ".");
}
----------
var nameList = ["Alice", "Fred", "Jean", "Steve"];
var ageList = [23, 32, 28, 24];
function ageLookup(name) {
for (var i = 0; i < nameList.length ; i++) {
if (nameList[i] == name) {
return ageList[i];
}
return "Could not find " + name + ".";
}
----------
function clearTextBoxes() {
var allInputs = document.getElementsByTagName("input");
for (var i = 0; i < allInputs.length; i++) {
if (allInputs[i].type == "text") {
allInputs[i].value = "";
}
}
}
----------
var salesArray = [[2300, 3105, 2909, 4800],
[1800, 1940, 2470, 4350],
[900, 1200, 1923, 3810]];
var total = 0;
for (var i = 0; i < salesArray.length; i++) {
for (var j = 0; j < salesArray[i].length; j++) {
total += salesArray[i][j];
}
}
Related examples in the same category