Skip to content

Commit

Permalink
[guide] Updated Arrays Section
Browse files Browse the repository at this point in the history
- corrected paragraph 4.4 (conversion from iterable to array)
- added paragraph 4.5 (conversion from array-like to array)
  • Loading branch information
maurozenoni authored and ljharb committed Jul 3, 2018
1 parent fe8de59 commit 5d1f4b9
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@ Other Style Guides
const itemsCopy = [...items];
```
<a name="arrays--from"></a><a name="4.4"></a>
- [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).
<a name="arrays--from">
<a name="arrays--from-iterable"></a><a name="4.4"></a>
- [4.4](#arrays--from-iterable) To convert an iterable object to an array, use spreads `...` instead of [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from).
```javascript
const foo = document.querySelectorAll('.foo');
Expand All @@ -382,8 +383,21 @@ Other Style Guides
const nodes = [...foo];
```
<a name="arrays--from-array-like"></a>
- [4.5](#arrays--from-array-like) Use [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) for converting an array-like object to an array.
```javascript
const arrLike = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };
// bad
const arr = Array.prototype.slice.call(arrLike);
// good
const arr = Array.from(arrLike);
```
<a name="arrays--mapping"></a>
- [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.
- [4.6](#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.
```javascript
// bad
Expand All @@ -394,7 +408,7 @@ Other Style Guides
```
<a name="arrays--callback-return"></a><a name="4.5"></a>
- [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`](https://eslint.org/docs/rules/array-callback-return)
- [4.7](#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`](https://eslint.org/docs/rules/array-callback-return)
```javascript
// good
Expand Down Expand Up @@ -441,7 +455,7 @@ Other Style Guides
```
<a name="arrays--bracket-newline"></a>
- [4.7](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines
- [4.8](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines
```javascript
// bad
Expand Down

0 comments on commit 5d1f4b9

Please sign in to comment.