开发者

Double-splitting server response returns incorrectly

开发者 https://www.devze.com 2023-04-13 06:37 出处:网络
This is the function to chop up the responseText from the server. I\'ve commented the data structures for clarity.

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 = [];
   开发者_如何学JAVA 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...


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(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消