Indexed arrays |
var objectArray = ["Saab", Date.now, myFunction]; |
cars[0] returns "Saab" |
var array = [] |
initialize empty array |
var array = new array(40) |
40 uninitialized elems! |
Associative arrays |
var person = {firstName:"John", lastName:"Doe", age:46}; |
person.lastName or person["lastName"] returns |
Looping indexed arrays |
for(var i = 0; i < arrayName.length; i++) { return arrayName[i]} |
Looping associative arrays |
for (var keyEtudiant in tabEtudiant) {print keyEtudiant+":"+tabEtudiant[keyEtudiant]} |
Array methods |
arrayName.toString() |
convert array contents to string |
arrayName.join(" seperator ") |
same as above: elem1 elem2 elem3 |
arrayName.pop() |
removes last elem |
arrayName.puh(elem) |
add new element to end of array |
arrayName.shift() |
removes first elem from array | returns value of arrayName[0] |
arrayName.unshift(elem) |
adds new elem to begining of array |
arrayName.length |
returns number of elems in array |
elete arrayName[0] |
changes first elem of array to undefined |
arrayName.splice(position, numElems, elemToAdd1, elementToAdd2) |
add nzw items to an array |
arrayName.splice(startingPos,numElemesToDelete) |
removes numberOfElems to delete starting from startingPos |
var pets = catsArray.concat(dogArray); |
joins two array together into one |
elem.innerHTML = arrayName; |
replaces HTML content of elem by string containing the elems of arrayName |
Sorting arrays |
arrayName.sort() |
sorts elems from less important to most important |
arrayName.reverse() |
sorts most important to less important |