Skip to content

Commit 9d32046

Browse files
trevorahrichardwestenra
authored andcommitted
Replace $.each with Array.prototype.forEach in JS3 (#339)
* Switch $.each with Array.prototype.forEach and so dropping support for ie8 * Clarify the location of the forEach function
1 parent f6ce9d6 commit 9d32046

5 files changed

Lines changed: 14 additions & 18 deletions

File tree

coaches/lesson-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ of a tutorial the student should be focussing on.
147147
* changing elements with `text`, `val`, and `addClass`
148148
* adding event handlers with `on`
149149
* changing css properties with `css`
150-
* `$.each`
150+
* `Array.prototype.forEach`
151151
* `$(document).ready`
152152
* Know how to write event handlers for keyboard and mouse events
153153
* Know how to change web pages in response to events

examples/tv-schedule/script.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function getGenres() {
2323
url: "http://www.bbc.co.uk/tv/programmes/genres.json"
2424
}).done(function(data) {
2525
console.log(data);
26-
$.each(data.categories, function(index, item) {
26+
data.categories.forEach(function(item) {
2727
$('#genres').append("<li id='"+ item.key +"'>" + item.title + "</li>");
2828
})
2929
}).fail(function() {
@@ -42,7 +42,7 @@ function getTomorrowsSchedule(genre) {
4242
}).done(function(data) {
4343
$(".spinner").remove();
4444
if (data.broadcasts.length > 0) {
45-
$.each(data.broadcasts, function(index, episode) {
45+
data.broadcasts.forEach(function(episode) {
4646
$("#programmes").append(processEpisode(episode));
4747
})
4848
} else {
@@ -63,7 +63,7 @@ function getUpcomingEpisodes(pid) {
6363
}).done(function(data) {
6464
$(".spinner").remove();
6565

66-
$.each(data.broadcasts, function(index, episode) {
66+
data.broadcasts.forEach(function(episode) {
6767
$("#programmes").append(processEpisode(episode));
6868
})
6969
}).fail(function() {

examples/wishlist/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function updateTotal() {
1515
}
1616

1717
$(document).ready(function(){
18-
$.each(wishes, function(index, element) {
18+
wishes.forEach(function(element) {
1919
addToList(element);
2020
});
2121
updateTotal();

js/lesson3/tutorial.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -444,26 +444,22 @@ button. Make it do the following things:
444444

445445
### Try it out
446446

447-
For this next part, we're going to use a feature of jQuery that isn't
448-
related to changing web pages. Open the javascript console. Start by
449-
defining an array:
447+
For this next part, we're going to use a function that belongs to arrays.
448+
Open the javascript console. Start by defining an array:
450449

451450
```js
452451
var words = ['these', 'are', 'some', 'words'];
453452
```
454453

455-
We're going to use the jQuery `$.each` function. This isn't related to
456-
the `$()` function, it takes an array and a function and calls the
457-
function once for each thing in the array. Try this:
454+
We're going to use your array's `forEach` function. You can use it to call another function once for each thing in the array. Try this:
458455

459456
```js
460-
$.each(words, function(index, word) {
461-
console.log('Position ' + index + ': ' + word)
457+
words.forEach(function(word) {
458+
console.log(word)
462459
});
463460
```
464461

465-
See how the function got called with `(0, 'these')`, then with `(1,
466-
'are')`, and so on? Make sure you understand what's happening
462+
See how the function got called with `'these'`, then with `'are'`, and so on? Make sure you understand what's happening
467463
here. Experiment with it, or ask your coach, if there's anything
468464
you're unsure about.
469465

@@ -493,7 +489,7 @@ Pick a few colour codes you like and store them in an array:
493489
var colors = [ '22ac5e', 'd68236', '770077' ];
494490
```
495491

496-
Now you can use a `$.each` inside your `ready` function to call
492+
Now you can use a `colors.forEach` inside your `ready` function to call
497493
`addBox` for each color in this array. When you reload the page, you
498494
should see that all these colours have been added, without you needing
499495
to click on anything.
@@ -652,7 +648,7 @@ Here are the things you learned about in exercise 2:
652648
3. $(document).ready() lets you write code that runs after your page
653649
has loaded.
654650

655-
4. $.each() lets you run some code for each thing in an array.
651+
4. forEach() lets you run some code for each thing in an array.
656652

657653
# Links
658654

js/lesson4/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ function retrieveGenres() {
289289
290290
> You can use `<title>` to display a humanly readable format of the Genre
291291
292-
As you can see from the console, the resulting objects are returned inside an Array. We want to iterate over the list using the `$.each( )` function and add each item to the `#genres` list, as a **list item**. As we need to have access to the `key` as well, we can set that as the list item's `id`.
292+
As you can see from the console, the resulting objects are returned inside an Array. We want to iterate over the list using the native Array `forEach( )` function and add each item to the `#genres` list, as a **list item**. As we need to have access to the `key` as well, we can set that as the list item's `id`.
293293

294294
Now that we have all the available genres, we can move on to making calls to the API using the genre to retrieve tomorrow's schedule!!
295295

0 commit comments

Comments
 (0)