Yes, you could use a multidimensional array - and then try to remember if myArray[0][1] or myArray[1][0] contains the value you are looking for...
I vote for this "object oriented" approach:
Code:
<html>
<head>
<script language="javascript">
function Item(id, name, price)
{
this.id = id;
this.name = name;
this.price = price;
}
function findItem(arr, id)
{
for(var i = 0; i < arr.length; i++)
{
if(arr[i].id == id)
{
return arr[i];
}
}
return null;
}
var arrItems = new Array();
arrItems[0] = new Item(123, 'A thing', '$99');
arrItems[1] = new Item(456, 'Another thing', '$55');
arrItems[2] = new Item(789, 'Stuff', '$1');
var id = 456;
var item = findItem(arrItems, id);
if(item)
{
document.write('The name of the item with id ' + id + ' is ' + item.name +
' and the price is ' + item.price);
}
else
{
document.write('There is no item with id ' + id);
}
</script>
</head>
<body>
</body>
</html>
Bookmarks