当您使用API进行搜索时,我正尝试删除在搜索中显示的所有重复播放器。当新搜索开始时,播放器将保持在桌面上,但是,某些API具有同一播放器的多个副本。隐藏和检查是否有其他玩家是重复的最佳方式是什么?
谢谢
function playerseachMSF() {
var searchPlayerFN = $("#searchFN").val().trim();
var searchPlayerLN = $("#searchLN").val().trim();
var searchedPlayer = "";
//allows user to only search for last name and have fist name be optional
if (searchPlayerFN == 0) {
var searchedPlayer = searchPlayerLN;
} else {
var searchedPlayer = searchPlayerFN + "-" + searchPlayerLN;
}
var playerQueryURL = "https://api.mysportsfeeds.com/v1.1/pull/nfl/2017-regular/active_players.json?" + "player=" + searchedPlayer;
$.ajax({
type: "GET",
url: playerQueryURL,
dataType: 'json',
async: true,
headers: {
"Authorization": "Basic " + btoa("chen" + ":" + "testing")
},
}).done(function(response) {
console.log("PlayerSearch MSF - " + playerQueryURL);
var playerResults = response.activeplayers.playerentry;
for (var i = 0; i < playerResults.length; i++) {
//removes any variables with undifined results
if (playerResults[i].team !== undefined) {
var playerResults_FN = playerResults[i].player.FirstName;
var playerResults_LN = playerResults[i].player.LastName;
var playerResults_Position = playerResults[i].player.Position;
var playerResults_Team = playerResults[i].team.Name;
var playerResults_City = playerResults[i].team.City;
var playerResults_CityAbbr = playerResults[i].team.Abbreviation;
var playerResults_IMG = playerResults[i].player.officialImageSrc;
}
if (playerResults[i].player.externalMapping !== null) {
var playerResults_ID = playerResults[i].player.externalMapping.ID;
}
//adds players that match searched name to table
$("#tableSearchList")
.append($("<tr>")
.attr("id", playerResults_ID)
.attr("data-FN", playerResults_FN)
.attr("data-LN", playerResults_LN)
.attr("class", "players")
.append( $("<td>").text(playerResults_FN + " " + playerResults_LN))
.append( $("<td>").text(playerResults_Team))
.append( $("<td>").text(playerResults_Position))
) //<tr> append
} //for loop
// on click for when a player is selected
$(".players").on("click", function() {
$("#resultsBox").show();
$("#searchPanel").hide(1000);
var playerID = $(this).attr("id");
var playerFN = $(this).attr("data-FN");
var playerLN = $(this).attr("data-LN");
arrestRecord(playerFN, playerLN);
playerDisplay(playerID);
displayPlayerStats(playerFN, playerLN);
playerFantasyStats(playerFN, playerLN)
})
答案 0 :(得分:0)
在添加新行之前,您可以检查表中是否已包含具有播放器ID的行。
if ($('#' + playerResults_ID).length === 0) {
$("#tableSearchList")
.append($("<tr>")
.attr("id", playerResults_ID)
.attr("data-FN", playerResults_FN)
.attr("data-LN", playerResults_LN)
.attr("class", "players")
.append( $("<td>").text(playerResults_FN + " " + playerResults_LN))
.append( $("<td>").text(playerResults_Team))
.append( $("<td>").text(playerResults_Position))
) //<tr> append
}
//else, player id already exists
答案 1 :(得分:0)
添加一系列现有玩家,并在添加新玩家之前进行比较。
function playerseachMSF() {
var searchPlayerFN = $("#searchFN").val().trim();
var searchPlayerLN = $("#searchLN").val().trim();
var searchedPlayer = "";
var alreadyPlaying = []
//allows user to only search for last name and have fist name be optional
if (searchPlayerFN == 0) {
var searchedPlayer = searchPlayerLN;
} else {
var searchedPlayer = searchPlayerFN + "-" + searchPlayerLN;
}
var playerQueryURL = "https://api.mysportsfeeds.com/v1.1/pull/nfl/2017-regular/active_players.json?" + "player=" + searchedPlayer;
$.ajax({
type: "GET",
url: playerQueryURL,
dataType: 'json',
async: true,
headers: {
"Authorization": "Basic " + btoa("chen" + ":" + "testing")
},
}).done(function(response) {
console.log("PlayerSearch MSF - " + playerQueryURL);
var playerResults = response.activeplayers.playerentry;
for (var i = 0; i < playerResults.length; i++) {
//removes any variables with undifined results
if (playerResults[i].team !== undefined) {
var playerResults_FN = playerResults[i].player.FirstName;
var playerResults_LN = playerResults[i].player.LastName;
var playerResults_Position = playerResults[i].player.Position;
var playerResults_Team = playerResults[i].team.Name;
var playerResults_City = playerResults[i].team.City;
var playerResults_CityAbbr = playerResults[i].team.Abbreviation;
var playerResults_IMG = playerResults[i].player.officialImageSrc;
}
if (playerResults[i].player.externalMapping !== null) {
var playerResults_ID = playerResults[i].player.externalMapping.ID;
}
if(alreadyPlaying.indexOf(playerResults_ID) == -1) {
alreadyPlaying.append(playerResults_ID)
//adds players that match searched name to table
$("#tableSearchList")
.append($("<tr>")
.attr("id", playerResults_ID)
.attr("data-FN", playerResults_FN)
.attr("data-LN", playerResults_LN)
.attr("class", "players")
.append( $("<td>").text(playerResults_FN + " " + playerResults_LN))
.append( $("<td>").text(playerResults_Team))
.append( $("<td>").text(playerResults_Position))
) //<tr> append
}
} //for loop
// on click for when a player is selected
$(".players").on("click", function() {
$("#resultsBox").show();
$("#searchPanel").hide(1000);
var playerID = $(this).attr("id");
var playerFN = $(this).attr("data-FN");
var playerLN = $(this).attr("data-LN");
arrestRecord(playerFN, playerLN);
playerDisplay(playerID);
displayPlayerStats(playerFN, playerLN);
playerFantasyStats(playerFN, playerLN)
})