filter array values by multiple values by using underscore
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js"></script>
<!--
filter array values by multiple values by using underscore
Useful to filter array values, large objects, using only matching values of a particular field
in this example, the code filters only managers from the employees object
list of names are used to filters
-->
JavaScript
var employees = [
{
id: 1,
name: 'Kumar',
designation:'Manager'
},
{
id: 2,
name: 'Mani',
designation:'Engineer'
},
{
id: 3,
name: 'Akash',
designation:'Manager'
},
{
id:4,
name: 'Sruthi',
designation:'Tester'
}
];
var managerNames =[ 'Kumar','Akash' ];
var Managers = _.filter(employees, function(i) {
return this.keys.indexOf(i.name) > -1 && i.id==3;
}, { "keys": managerNames });
console.log(Managers);