Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下划线.js的替代解决方案_Javascript_Jquery_Underscore.js - Fatal编程技术网

Javascript 下划线.js的替代解决方案

Javascript 下划线.js的替代解决方案,javascript,jquery,underscore.js,Javascript,Jquery,Underscore.js,我已经创造了一个游戏,是基于一个网格填充的话 在我的代码中,我使用了一点underline.js来帮助我的文字适应网格中的可用空间,而不会突破网格的障碍 我知道这是一个非常强大的java脚本,我个人对此没有任何问题。但是我的团队经理希望通过一些jQuery来摆脱它,这些jQuery将提供相同的解决方案,因为只有一个函数,可以节省我拥有整个库的时间。我将如何用一些jQuery替换它 function getWordToFitIn(spaceAvail, wordlist) { var fo

我已经创造了一个游戏,是基于一个网格填充的话

在我的代码中,我使用了一点underline.js来帮助我的文字适应网格中的可用空间,而不会突破网格的障碍

我知道这是一个非常强大的java脚本,我个人对此没有任何问题。但是我的团队经理希望通过一些jQuery来摆脱它,这些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;
}
函数getWordToFitIn(spaceAvail,wordlist){ var foundIndex=-1; var answer=\查找(单词列表、函数(单词、索引){
if(word.length据我所知,您使用的唯一下划线方法是
.find
。但我认为您并没有按预期使用它。看起来您只是在循环并在满足条件时返回true

如果没有旧版支持,可以使用本机
forEach
,或者使用垫片。或者可以使用
jQuery.each
方法

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

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

研究源代码。我已经看过了。我知道它在做什么,只是不知道如何替换它。@user1689607哪一部分你不明白?(好像我以前和你有过这样的对话。)如果没有足够的空间,我不明白如何使填充网格的单词占据新的位置。@user1689607只需使用
for
循环或典型的jQuery迭代器。如果你不知道那是什么,那么你需要学习jQuery。谢谢。最后一位呢?它保持不变吗@David@Milo-最后一点是什么就我所见,t是本机javascript。@米洛-J:也许你应该试试看。@David:在第一块中,不要使用
answer=true
,而是使用
answer=word
@Milo-J这些代码片段应该能够替换现有的下划线循环,就像你问的那样。为什么它“不起作用”这取决于你自己去发现,或者至少自己做一些像样的调试,这样你就知道该问什么了
$.each(wordlist, function (index, word) {
    if (word[0] == " ") {
        foundIndex = index;
        return false;
    }
});