Skip to content

Rework javascript-101/arrays.md #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added remarks by addy
  • Loading branch information
Stefan Baumgartner committed Mar 29, 2012
commit 86bf87f1fb60e271b930c5820fe172495857d17d
45 changes: 44 additions & 1 deletion content/javascript-101/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,24 @@ var myArray = [ 'hello', 'world', '!'];
console.log(myArray.length); // logs 3
</javascript>

You will need the length property for looping through an array:

<javascript caption="For loops and arrays - a classic">
var myArray = ['hello', 'world', '!'];
for(var i = 0; i < myArray.length; i = i + 1) {
console.log(myArray[i]);
}
</javascript>

Except when you are using for ... in loops:

<javascript caption"For loops and arrays - alternate method">
var myArray = ['hello', 'world', '!'];
for(var i in myArray) {
console.log(myArray[i]);
}
</javascript>

## concat

With 'concat', you can concatenate two or more arrays
Expand Down Expand Up @@ -198,4 +209,36 @@ var myArray = [];
myArray.unshift(0); // [ 0 ]
myArray.unshift(2); // [ 2 , 0 ]
myArray.unshift(7); // [ 7 , 2 , 0 ]
</javascript>
</javascript>

## forEach

In modern browsers, like Chrome, Firefox and Internet Explorer 9 it is possible to traverse
through arrays by a so called 'forEach' method, where you pass a function which is called
for each element in your array.

The function takes up to three arguments:
* element - The element itself
* index - The index of this element in the array
* array - The array itself

All of the are optional, but you will need at least the 'element' parameter in most cases.

<javascript caption="native forEach">
function printElement(elem) {
console.log(elem);
}

function printElementAndIndex(elem, index) {
console.log("Index " + index + ": " + elem);
}

function negateElement(elem, index, array) {
array[index] = -elem;
}

myArray = [1, 2, 3, 4, 5];
myArray.forEach(printElement); //prints all elements to the console
myArray.forEach(printElementAndIndex); //prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ...
myArray.forEach(negateElement); // myArray is now [-1, -2, -3, -4, -5]
</javascript>