Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return Array Instead of String

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading