Skip to content

Commit e16b03b

Browse files
bl00mberljharb
authored andcommitted
[guide] require array spread operator or Array.from
Fixes #1084.
1 parent 0aab14c commit e16b03b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,31 @@ Other Style Guides
364364
```
365365

366366
<a name="arrays--from"></a><a name="4.4"></a>
367-
- [4.4](#arrays--from) To convert an array-like object to an array, use [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from).
367+
- [4.4](#arrays--from) To convert an array-like object to an array, use spreads `...` instead of [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from).
368368

369369
```javascript
370370
const foo = document.querySelectorAll('.foo');
371+
372+
// good
371373
const nodes = Array.from(foo);
374+
375+
// best
376+
const nodes = [...foo];
377+
```
378+
379+
<a name="arrays--mapping"></a>
380+
- [4.5](#arrays--mapping) Use [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) instead of spread `...` for mapping over iterables, because it avoids creating an intermediate array.
381+
382+
```javascript
383+
// bad
384+
const bar = [...foo].map(bar);
385+
386+
// good
387+
const bar = Array.from(foo, bar);
372388
```
373389

374390
<a name="arrays--callback-return"></a><a name="4.5"></a>
375-
- [4.5](#arrays--callback-return) Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following [8.2](#arrows--implicit-return). eslint: [`array-callback-return`](http://eslint.org/docs/rules/array-callback-return)
391+
- [4.6](#arrays--callback-return) Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following [8.2](#arrows--implicit-return). eslint: [`array-callback-return`](http://eslint.org/docs/rules/array-callback-return)
376392

377393
```javascript
378394
// good
@@ -420,8 +436,8 @@ Other Style Guides
420436
});
421437
```
422438
423-
<a name="arrays--bracket-newline"></a>
424-
- [4.6](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines
439+
<a name="arrays--bracket-newline"></a>
440+
- [4.7](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines
425441
426442
```javascript
427443
// bad
@@ -2511,7 +2527,7 @@ Other Style Guides
25112527
25122528
## Commas
25132529
2514-
<a name="commas--leading-trailing"></a><a name="19.1"></a>
2530+
<a name="commas--leading-trailing"></a><a name="19.1"></a>
25152531
- [20.1](#commas--leading-trailing) Leading commas: **Nope.** eslint: [`comma-style`](http://eslint.org/docs/rules/comma-style.html) jscs: [`requireCommaBeforeLineBreak`](http://jscs.info/rule/requireCommaBeforeLineBreak)
25162532
25172533
```javascript

0 commit comments

Comments
 (0)