mirror of
https://github.com/airbnb/javascript.git
synced 2026-04-25 03:00:19 -04:00
[guide] Updated Arrays Section
- corrected paragraph 4.4 (conversion from iterable to array) - added paragraph 4.5 (conversion from array-like to array)
This commit is contained in:
committed by
Jordan Harband
parent
fe8de59977
commit
5d1f4b9a49
24
README.md
24
README.md
@@ -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');
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user