双分割服务器响应返回不正确

发布于 2024-12-10 12:47:19 字数 1434 浏览 0 评论 0原文

这是从服务器分割 responseText 的函数。为了清楚起见,我对数据结构进行了评论。

function chopUpResponse(serverResponse)
{
        //  serverResponse: ("a|b|c@d|e|f@g|h|i")
    console.log("chopUpResponse called");
    var ranges = [];
    var firstSplit = [];
    var secondSplit = [];

    firstSplit = serverResponse.split("@");
    console.log("firstSplit: " + firstSplit);
        //  result: ("a|b|c", "d|e|f", "g|h|i")

    for (var i=0; i<firstSplit.length; i++)
    {
        secondSplit = secondSplit.concat(firstSplit[i].split("|"));
        console.log("secondSplit: " + secondSplit);
            //  desired: ("a","b","c","d","e","f","g","h","i")
    }

    for (var j=0; j<firstSplit.length; j++)
    {
            console.log("j: "+j);
            for (var k=0; k<3; k++)
            //  would be nice to generate the '3' dynamically from the @ delimiter
            {
                    console.log("k: "+k);
                    ranges[j,k] = secondSplit[j+1*k];
                            //  the +1 so we're not * by zero!
                            //  j+1*k should equal secondSplit.length...
                    console.log("ranges["+j+","+k+"]: " + ranges[j,k]);
            }
    }   
    return ranges;
}

最后一个函数本质上是为了构建一个二维数组。由于某种原因,我错过了,而不是我所期待的,我得到了这个:

ranges["a,b,c","b,c,d","c,d,e"]

我错过了什么?有更好的方法吗?我希望我可以在这个脚本中使用一些 PHP,因为它在处理此类数据方面要好得多......

This is the function to chop up the responseText from the server. I've commented the data structures for clarity.

function chopUpResponse(serverResponse)
{
        //  serverResponse: ("a|b|c@d|e|f@g|h|i")
    console.log("chopUpResponse called");
    var ranges = [];
    var firstSplit = [];
    var secondSplit = [];

    firstSplit = serverResponse.split("@");
    console.log("firstSplit: " + firstSplit);
        //  result: ("a|b|c", "d|e|f", "g|h|i")

    for (var i=0; i<firstSplit.length; i++)
    {
        secondSplit = secondSplit.concat(firstSplit[i].split("|"));
        console.log("secondSplit: " + secondSplit);
            //  desired: ("a","b","c","d","e","f","g","h","i")
    }

    for (var j=0; j<firstSplit.length; j++)
    {
            console.log("j: "+j);
            for (var k=0; k<3; k++)
            //  would be nice to generate the '3' dynamically from the @ delimiter
            {
                    console.log("k: "+k);
                    ranges[j,k] = secondSplit[j+1*k];
                            //  the +1 so we're not * by zero!
                            //  j+1*k should equal secondSplit.length...
                    console.log("ranges["+j+","+k+"]: " + ranges[j,k]);
            }
    }   
    return ranges;
}

The last function is intended to essentially build a two-dimensional array. For some reason that I am missing, instead of what I'm expecting, I get this:

ranges["a,b,c","b,c,d","c,d,e"]

What am I missing? Is there a better way to do this? I wish I could just bust out some PHP inside this script because it's a hell of a lot better at handling this kind of data...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦晓ヶ微光ヅ倾城 2024-12-17 12:47:19
function chopUpResponse(serverResponse)
{
    //  serverResponse: ("a|b|c@d|e|f@g|h|i")
    console.log("chopUpResponse called");
    //var ranges = [];
    var firstSplit = [];
    //var secondSplit = [];

    firstSplit = serverResponse.split("@");
    console.log("firstSplit: " + firstSplit);
    //  result: ("a|b|c", "d|e|f", "g|h|i")

    for (var i=0; i<firstSplit.length; i++)
    {
        //secondSplit = secondSplit.concat(firstSplit[i].split("|"));
        firstSplit[i] = firstSplit[i].split("|");
        //console.log("secondSplit: " + secondSplit);
        //  desired: ("a","b","c","d","e","f","g","h","i")
    }

    //for (var j=0; j<firstSplit.length; j++)
    //{
    //        console.log("j: "+j);
    //        for (var k=0; k<3; k++)
    //        //  would be nice to generate the '3' dynamically from the @ delimiter
    //        {
    //                console.log("k: "+k);
    //                ranges[j,k] = secondSplit[j+1*k];
    //                        //  the +1 so we're not * by zero!
    //                        //  j+1*k should equal secondSplit.length...
    //                console.log("ranges["+j+","+k+"]: " + ranges[j,k]);
    //        }
    //}   
    //return ranges;
    return firstSplit;
}
function chopUpResponse(serverResponse)
{
    //  serverResponse: ("a|b|c@d|e|f@g|h|i")
    console.log("chopUpResponse called");
    //var ranges = [];
    var firstSplit = [];
    //var secondSplit = [];

    firstSplit = serverResponse.split("@");
    console.log("firstSplit: " + firstSplit);
    //  result: ("a|b|c", "d|e|f", "g|h|i")

    for (var i=0; i<firstSplit.length; i++)
    {
        //secondSplit = secondSplit.concat(firstSplit[i].split("|"));
        firstSplit[i] = firstSplit[i].split("|");
        //console.log("secondSplit: " + secondSplit);
        //  desired: ("a","b","c","d","e","f","g","h","i")
    }

    //for (var j=0; j<firstSplit.length; j++)
    //{
    //        console.log("j: "+j);
    //        for (var k=0; k<3; k++)
    //        //  would be nice to generate the '3' dynamically from the @ delimiter
    //        {
    //                console.log("k: "+k);
    //                ranges[j,k] = secondSplit[j+1*k];
    //                        //  the +1 so we're not * by zero!
    //                        //  j+1*k should equal secondSplit.length...
    //                console.log("ranges["+j+","+k+"]: " + ranges[j,k]);
    //        }
    //}   
    //return ranges;
    return firstSplit;
}
揪着可爱 2024-12-17 12:47:19
function chopUpResponse(res) {
    // Split on the '@'
    var split = res.split("@"),
        // Loop counter
        i = 0,
        // Number of arrays produced by splitting on '@'
        len = split.length,
        // Will be each array produced by splitting on '@'
        // inside the loop body
        part,
        // Holds the results
        ret = [];

    for ( ; i < len; i++ ) {
        part = split[ i ];
        // Array::push adds items to an array. Function::apply unpacks an array
        // as indidual arguments
        ret.push.apply( ret, part.split("|") )
    }

    // Over and out.
    return ret;
}

希望有帮助!干杯。

function chopUpResponse(res) {
    // Split on the '@'
    var split = res.split("@"),
        // Loop counter
        i = 0,
        // Number of arrays produced by splitting on '@'
        len = split.length,
        // Will be each array produced by splitting on '@'
        // inside the loop body
        part,
        // Holds the results
        ret = [];

    for ( ; i < len; i++ ) {
        part = split[ i ];
        // Array::push adds items to an array. Function::apply unpacks an array
        // as indidual arguments
        ret.push.apply( ret, part.split("|") )
    }

    // Over and out.
    return ret;
}

hope that helps! cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文