var GAME1NUM = "";
if (json.participantIdentities[0].player.summonerName == ACTUALNAME) {
var GAME1NUM = 0;
} else if (json.participantIdentities[1].player.summonerName == ACTUALNAME) {
var GAME1NUM = 1;
} else if (json.participantIdentities[2].player.summonerName == ACTUALNAME) {
var GAME1NUM = 2;
} else if (json.participantIdentities[3].player.summonerName == ACTUALNAME) {
var GAME1NUM = 3;
} else if (json.participantIdentities[4].player.summonerName == ACTUALNAME) {
var GAME1NUM = 4;
} else if (json.participantIdentities[5].player.summonerName == ACTUALNAME) {
var GAME1NUM = 5;
} else if (json.participantIdentities[6].player.summonerName == ACTUALNAME) {
var GAME1NUM = 6;
} else if (json.participantIdentities[7].player.summonerName == ACTUALNAME) {
var GAME1NUM = 7;
} else if (json.participantIdentities[8].player.summonerName == ACTUALNAME) {
var GAME1NUM = 8;
} else if (json.participantIdentities[9].player.summonerName == ACTUALNAME) {
var GAME1NUM = 9;
}
需要帮助来简化重复的代码
真的请帮忙,如果您不愿意的话可以,但是请您帮忙
最佳答案
使用ES6中的Array#findIndex
:
var GAME1NUM = json.participantIdentities.findIndex(function(val) {
return val.player.summonerName === ACTUALNAME;
});
如果需要更多的浏览器兼容性,请使用polyfill。