Lodash Where Inclusive Or
Joining together multiple _.where() filters with an "inclusive or" condition.
by Adam Boduch
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
JavaScript
$(function() {
// Bogus sample data.
var data = [
{ age: 23, gender: "male" },
{ age: 25, gender: "female" },
{ age: 25, gender: "male", active: true },
{ age: 26, gender: "female" },
{ age: 23, gender: "male" },
{ age: 25, gender: "female", active: true }
],
// With help from _.uniq(), we can join together
// multiple _.where() conditions as though they
// were "inclusive or"ed together.
result = _.uniq( ( [] ).concat(
_.where( data, { gender: "female" } ),
_.where( data, { age: 25, active: true } ) ) );
$( "<p/>" ).html( result.length ).appendTo( "body" );
});