Skip to content

Commit 1df9825

Browse files
sara-gudemanljharb
authored andcommitted
[guide] add map example to iterators
1 parent a56f239 commit 1df9825

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,6 @@ Other Style Guides
13091309
for (let num of numbers) {
13101310
sum += num;
13111311
}
1312-
13131312
sum === 15;
13141313
13151314
// good
@@ -1320,6 +1319,19 @@ Other Style Guides
13201319
// best (use the functional force)
13211320
const sum = numbers.reduce((total, num) => total + num, 0);
13221321
sum === 15;
1322+
1323+
// bad
1324+
const modified = [];
1325+
for (let i = 0; i < numbers.length; i++) {
1326+
modified.push(numbers[i] + 1);
1327+
}
1328+
1329+
// good
1330+
const modified = [];
1331+
numbers.forEach(num => modified.push(num + 1));
1332+
1333+
// best (keeping it functional)
1334+
const modified = numbers.map(num => num + 1);
13231335
```
13241336
13251337
<a name="generators--nope"></a><a name="11.2"></a>

0 commit comments

Comments
 (0)