Lodash in the real world: Sample

Joe L. Wroten
1 min readDec 9, 2015

--

A powerful and reliable ally to many recent projects of mine has been the super hero Lodash. It’s ability to deal with many forms of data in an intelligent and intuitive way can change an otherwise ugly problem into a beautifully simple solution.

Scenario

Our designer pitched a filter-as-you-type search bar for our sidebar. It’s placeholder is intended to show some potential valid search terms.

Of course we could write in some static examples, but what a great way to recommend new content each time a user uses the site by randomly selecting some available items as search examples!

Solution

Lodash makes this quite easy with _.sample(). Quite simply, when given an array (or collection) and a number of results expected, you’ll receive a shortened sampled array of that many results.

So there you go, a real life scenario where Lodash has made at least my day a little easier. Perhaps it can for you as well.

--

--

Joe L. Wroten

I’m an edge riding web tech architect aimed at realizing the best tools to communicate valuable information to the people who need it.