2

I am trying to get an array response from my getTeam() function so I can properly filter my DataTable to show more than one team for the current Player (if they are on more than one team).

DataTables Filter:

  $.fn.dataTable.ext.search.push( //creating my own filter function for the table
        function(settings, searchData, index, rowData, counter, statusClass) { 

            var userName = searchData[1]; //created a var to search through the 3rd column (0 index) which contains all of the Monday Dates
            var searchTeam = searchData[0];
            // Get Datatables API
            var api = $.fn.dataTable.Api('#myTable'); 
            if (settings.sTableId === 'myTable' ) {
            if(userName === thisUserTitle){
            return true;
            } else {
              return false;
           }
           
          }
          if (settings.sTableId === 'certificateTable' ) {
            if(currentTeam === searchTeam){
            return true;
            } else {
              return false;
           }
           
          }
          return true;
        }
    );

The original snippet that returns just the first team:

var teamData = [{
  "Team" : "Team 1",
  "Players" :[ 
    "Beerus Dev",
    "Goten Dev",
    "Trunks Dev",
    "Majin Bu Dev"

  ]
},
{
  "Team" : "Team 2",
  "Players" : [
    "Beerus Dev",
    "Shap Dev",
    "Krillin Dev"
  ]
}
];


function getTeam() {

  const team = teamData.find(team => team.Players.includes(currentUser));
  return team.Team;
}

var currentUser = "Beerus Dev";
var currentTeam = getTeam(currentUser);
 
console.log(currentTeam);
 

To get the array response, I have tried using the .filter() as opposed to the .find() but I end up getting an empty array as my response.

var teamData = [{
  "Team" : "Team 1",
  "Players" :[ 
    "Beerus Dev",
    "Goten Dev",
    "Trunks Dev",
    "Majin Bu Dev"

  ]
},
{
  "Team" : "Team 2",
  "Players" : [
    "Beerus Dev",
    "Shap Dev",
    "Krillin Dev"
  ]
}
];

var currentUser = "Beerus Dev";

const team = teamData.filter(function(team, i){
    return teamData[i].Players.includes(currentUser);
})

console.log(team)

4
  • Sorry, that was a typo on my part. That provides a response, but it returns the entire teamData instead of for in this example ["Team 1", "Team 2"] Commented Dec 1, 2021 at 15:37
  • i ran that code exactly as you posted (the filter version), and got an array with two entries in it. There must be something broken in your code somewhere else. This code works. Commented Dec 1, 2021 at 15:37
  • You can try reduce. Commented Dec 1, 2021 at 15:39
  • Cannot reproduce. Commented Dec 1, 2021 at 15:39

1 Answer 1

2

Simplify this code:

var teamData = [{
  "Team" : "Team 1",
  "Players" :[ 
    "Beerus Dev",
    "Goten Dev",
    "Trunks Dev",
    "Majin Bu Dev"

  ]
},
{
  "Team" : "Team 2",
  "Players" : [
    "Beerus Dev",
    "Shap Dev",
    "Krillin Dev"
  ]
}
];

var currentUser = "Beerus Dev";
const team = teamData.filter(team => team.Players.includes(currentUser)).map(team => team.Team);

console.log(team)

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

5 Comments

Or even further: filter(({ Players }) => Players.includes(currentUser)). This doesn't really answer the question tho
There is no limit to refactor))
See this returns the entire dataset I am trying to filter from, I need to get Team 1, Team 2 as the only items returned.
Just saw your edit, this is exactly what I was trying to do. Many thanks
You are welcome))

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.