I am going to research and document best practice to use js arrays.
Declare an array in JavaScript:
var myarray = [];
To assign a value to the array:
myarray[0] = “The first value”;
myarray[1] = “The second value”;
to insert a value at end of array:
myarray.push(“The third value”);
myarray.push(“The fourth”, “The fifth”);
The array.toString() will list the variables separated by a comma
Suppose I have a list of items that I would like to put into an array:
myarrayfromlist=[];
- My list item one
- My list item two
- My list item three
$("#mylist li").each(
function(){
myarrayfromlist.push($(this).html())
}
);
For a less complex situation where you don’t need custom keys, a standard array can be written as so… var coinsIhave = [1950, 1960, 1970, 1980, 1990, 2000, 2010];
then coinsIhave[0] is 1950.
How to I access the key of an item in the array, for example, what key is 1980?
Well, unfortunatley JavaScript doesn’t have a function for that. We can make our own, and in a less complex scenario, this would be ok.
[code]function findKey(whatkey, whatarray){
for(a=0;a<whatarray.length;a++){
if(whatarray[a].id==whatkey){
$(“#debuglog”).append(“<p>Array Item:”+whatarray[a].id+” || Key: ” + a +”</p>”);
return a;}
}
}[/code]
So if I were to execute findKey(coinsIhave, ‘1980’) it would return 3.
But if I want to know what kind of coin it is, as well as the year, I would like to use a dictionary object or associated array.
var coinsIhave=new Object();
coinsIhave.silverdollar=”1950″;
coinsIhave.centenialquarter=”1960″;
Although they can be called associated arrays, we are creating a JavaScript object and assigning properties. These properties can be assigned and accessed using dot or bracket notation. It is important to note that any property name that is not a valid JavaScript identifier (for example, a property name that has space or dash, or starts with a number) can only be accessed using the square bracket notation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FWorking_with_Objects#Indexing_object_properties
It is possible to “fake” a multidimensional array in JavaScript, using notation such as this:
var thecollegechoices=[[“CWU”,”Central University”], [“EWU”,”Eastern Wa University”]];
You would then access the data as so:
thecollegechoices[0][0] would contain the text “CWU”
thecollegechoices[0][1] would contain the text “Central University”
thecollegechoices[1][0] would contain the text “EWU”
thecollegechoices[1][1] would contain the text “Eastern Wa University”
So as we loop through the index of the array, we see the pattern that the abreviations would be in the 0, and the college name would be in the 1. This is going to come in very handy over the course of project development.
After reading the mozzilla docs, I have found a great way to loop through the array to use the items in the array:
[code]
var a = [“dog”, “cat”, “hen”];
for (var i = 0, item; item = a[i++];) {
// Do something with item
console.log(item);
}
[/code]
Note that this trick should only be used for arrays which you know do not contain “falsy” values (arrays of objects or DOM nodes for example). If you are iterating over numeric data that might include a 0 or string data that might include the empty string you should use the i, len
idiom instead:
[code]
for (var i = 0, len = a.length; i < len; i++) {
// Do something with a[i]
}[/code]
[code]for (var i in a) {
// Do something with a[i]
}[/code]