I'm trying to filter out "Players" by their TeamID which is an array, based on the selected checkboxes in a multi-select (Bootstrap Multiselect to be precise).
I had it working but the requirements changed and players can be in multiple teams. (I'm sorry I have no JSFiddle / CodePen, Bootstrap Multiselect has no CDN and both of them aren't playing nice)
This is what I had for individual teams which worked.
var temp = [];
_.each(selected, function(i){
temp.push(_.filter(allPlayers, function(obj){
return obj.TeamID == i;
}));
});
However I need to filter an array by an array.
JSON
var allPlayers = [{
"TeamID": [100001, 100002],
"PlayerID": 1,
"PlayerName" : "Pete Tong"
},
{
"TeamID": [100001, 100002],
"PlayerID": 2,
"PlayerName" : "Will Chamberlain"
},
{
"TeamID": [100002, 100003],
"PlayerID": 3,
"PlayerName" : "Jane Doe"
},
{
"TeamID": [100004],
"PlayerID": 4,
"PlayerName" : "John Doe"
}];
I've tried Filter two different structured arrays underscore js but it doesn't seem to work for my solution.
Selected Array
var teams = $('#team-list option:selected');
var selected = [];
$(teams).each(function(index, team){
selected.push($(this).val());
});