Updated longestItem + tests
This commit is contained in:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user