I saved the day with math!

in code


(OR: How to determine whether one array contains a subset of another in JavaScript)

Web development in the Rails universe is like code Lego: pick the right Bootstrap classes, fiddle the jQuery selectors and align the sprockets, among other such crap. This is fully as intensive (and boring) as any other area of programming, but rarely do I have to resort to serious math to solve a problem.

But today…today was glorious! There were two lists, Leftie List and Rightie List, which were properties of an object. We wanted the parent of a Leftie List to be displayed if it contained a subset of all elements in Rightie List. This led to a fun hour of set algebra and doodled Venn diagrams.

Behold:

lefie.filter(element => rightie.indexOf(element) !== -1).length === rightie.length

Or if you use Lodash:

_.isMatch(_.intersection(leftie, rightie), rightie);

It was random, unexpected and fun.



A Summer of Running

in running

Achill Half Marathon

in running


Your email address will not be published. Required fields are marked *