0

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());
 });
0

2 Answers 2

1

if you use filter(), you don't need an extra outside variable:

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"
}];



var selected=[100003, 100004]; // change this 

var filtered= allPlayers.filter(function(a){
    return selected.some(function(team){
      return a.TeamID.indexOf(team)!==-1;
    });
});

alert(JSON.stringify(filtered, null, "\t"));

demo: http://pagedemos.com/qejbsz722hs3/2

edit: added many to many searching.

Sign up to request clarification or add additional context in comments.

3 Comments

Much cleaner, however I'm afraid to say that selected is an array of TeamIDs. This is is what I'm struggling with :)
@Jezzabeanz: oops, i didn't realize both terms and teams were array-shaped, no problem though, it's still simple and that's a minor adjustment; answer updated.
It's okay, I should have put that in my code. Ah you're amazing @dandavis, works wonderfully. Would it be weird if I told you I love you?
1

This should work:

var temp = [];

_.each(selected, function(i){
            temp.push(_.filter(allPlayers, function(obj){
                return obj.TeamID.indexOf(i) !== -1;
            }));
});

Rather than comparing if the TeamID equals the selected id, you can check if the TeamID array contains the selected id.

3 Comments

Sorry @SlashmanX it doesn't seem to be working. When I output the result of obj.TeamID.indexOf(i) everything is -1.
Ah, that could be because you're comparing a string to a number. Is selected an array of strings (Seeing as you're using .val() to populate it). You could do ....indexOf(parseInt(i, 10)) and see if that works?
You're right SlashmanX, your solution works too. I wish I could mark both as correct. Sorry buddy, I'm grateful for your time though, so, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.