From 5d1f4b9a49ee7def65343f5efccfcce7723b9b4b Mon Sep 17 00:00:00 2001 From: Mauro Zenoni <25953912+maurozenoni@users.noreply.github.com> Date: Tue, 3 Jul 2018 11:51:37 +0200 Subject: [PATCH] [guide] Updated Arrays Section - corrected paragraph 4.4 (conversion from iterable to array) - added paragraph 4.5 (conversion from array-like to array) --- README.md | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7912c73cfc..54cb2c0d4c 100644 --- a/README.md +++ b/README.md @@ -369,8 +369,9 @@ Other Style Guides const itemsCopy = [...items]; ``` - - - [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). + + + - [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]; ``` + + - [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); + ``` + - - [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 ``` - - [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 ``` - - [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