From 6d0acc6a7ec8550e6cf313ab0ee58b976e77718a Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Mon, 13 Aug 2018 05:39:23 +0000 Subject: [PATCH] Travis build: 218 --- README.md | 4 ++-- docs/array.html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index da1f64dc8..808d6da55 100644 --- a/README.md +++ b/README.md @@ -1856,10 +1856,10 @@ last([1, 2, 3]); // 3 Takes any number of iterable objects or objects with a `length` property and returns the longest one. -Use `Array.sort()` to sort all arguments by `length`, return the first (longest) one. +Use `Array.reduce()` to collect the longest element. Returns [] for empty array. ```js -const longestItem = (...vals) => [...vals].sort((a, b) => b.length - a.length)[0]; +const longestItem = (...vals) => [...vals].reduce((a, x) => (a.length > x.length ? a : x), []); ```
diff --git a/docs/array.html b/docs/array.html index 8995de3db..33f5477b2 100644 --- a/docs/array.html +++ b/docs/array.html @@ -268,7 +268,7 @@ JSONtoCSV([{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }], ['a', 'b'], ';'); // 'a;b\n"1";"2"\n"3";"4"\n"6";""\n"";"7"'

last

Returns the last element in an array.

Use arr.length - 1 to compute the index of the last element of the given array and returning it.

const last = arr => arr[arr.length - 1];
 
last([1, 2, 3]); // 3
-

longestItem

Takes any number of iterable objects or objects with a length property and returns the longest one.

Use Array.sort() to sort all arguments by length, return the first (longest) one.

const longestItem = (...vals) => [...vals].sort((a, b) => b.length - a.length)[0];
+

longestItem

Takes any number of iterable objects or objects with a length property and returns the longest one.

Use Array.reduce() to collect the longest element. Returns [] for empty array.

const longestItem = (...vals) => [...vals].reduce((a, x) => (a.length > x.length ? a : x), []);
 
longestItem('this', 'is', 'a', 'testcase'); // 'testcase'
 longestItem(...['a', 'ab', 'abc']); // 'abc'
 longestItem(...['a', 'ab', 'abc'], 'abcd'); // 'abcd'