Updated longestItem + tests

This commit is contained in:
Angelos Chalaris
2018-08-14 20:55:06 +03:00
parent f8d3c5ccf5
commit c869d995f1
3 changed files with 29 additions and 4 deletions

View File

@ -1,11 +1,13 @@
### longestItem
Takes any number of iterable objects or objects with a `length` property and returns the longest one.
If multiple objects have the same length, the first one will be returned.
Returns `undefined` if no arguments are provided.
Use `Array.reduce()` to collect the longest element. Returns [] for empty array.
Use `Array.reduce()`, comparing the `length` of objects to find the longest one.
```js
const longestItem = (...vals) => [...vals].reduce((a, x) => (a.length > x.length ? a : x), []);
const longestItem = (val, ...vals) => [val, ...vals].reduce((a, x) => x.length > a.length ? x : a);
```
```js