gpt4 book ai didi

javascript - underscore.js 的替代解决方案

转载 作者:行者123 更新时间:2023-11-30 18:15:47 26 4
gpt4 key购买 nike

我创建了一个基于填充文字的网格的游戏

在我的代码中,我有一小段 underscore.js 可以帮助我的文字适应网格中的可用空间,而不会破坏网格障碍。

我知道它是非常强大的 java 脚本,我个人对它没有任何问题。但是我的团队经理想摆脱它,因为一些 jQuery 将提供相同的解决方案,因为只有一个功能,并且可以节省我拥有整个库的时间。我如何用一些 jQuery 替换它?

function getWordToFitIn(spaceAvail, wordlist) {
var foundIndex = -1;
var answer = _.find(wordlist, function (word, index) {
if (word.length <= spaceAvail) {
foundIndex = index;
return true;
}
});
if (foundIndex == -1) {
answer = getXSpaces(spaceAvail);
_.find(wordlist, function (word, index) {
if (word[0] == " ") {
foundIndex = index;
return true;
}
});
}
if (foundIndex != -1) {
wordlist.splice(foundIndex, 1);
}
return answer;
}

最佳答案

据我所知,您使用的唯一下划线方法是_.find。但我认为您没有按预期使用它。看起来您只是在循环并在满足条件时返回 true。

如果您没有遗留支持,您可以使用原生的 forEach,或者使用 shim。或者您可以使用 jQuery.each 方法。

第一个循环可能(我不是 100% 确定 answer 变量)是这样写的:

var answer;
$.each(wordlist, function(index, word) {
if (word.length <= spaceAvail) {
foundIndex = index;
answer = word;
return false; // stops the loop
}
});

第二个:

$.each(wordlist, function (index, word) {
if (word[0] == " ") {
foundIndex = index;
return false;
}
});

关于javascript - underscore.js 的替代解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291307/

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