双拆分服务器响应返回错误

时间:2011-10-14 17:20:35

标签: javascript arrays split sequence

这是从服务器中删除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,因为它在处理这种数据方面要好得多......

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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;
}
希望有所帮助!欢呼声。