双分割服务器响应返回不正确
这是从服务器分割 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望有帮助!干杯。
hope that helps! cheers.