gpt4 book ai didi

javascript - 谁能帮我简化一下?

转载 作者:行者123 更新时间:2023-12-02 13:47:58 26 4
gpt4 key购买 nike

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

需要帮助简化感觉重复的代码

真的请帮忙,如果你不帮忙也没关系,但如果你能请伙计

最佳答案

使用Array#findIndex来自 ES6:

var GAME1NUM = json.participantIdentities.findIndex(function(val) {
return val.player.summonerName === ACTUALNAME;
});

如果您需要更多浏览器兼容性,请使用polyfill .

关于javascript - 谁能帮我简化一下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com