我从API回调中收到大量数据,如下所示: response.endpoints [0] ..,response.endpoints [1] ..,response.endpoints [2] .. etc
如何获取和设置括号[]中的数字的变量,或直接提取此数字。
答案 0 :(得分:0)
试试这样:
var a="response.endpoints[0].., response.endpoints[1].., response.endpoints[2]..";
var b=a.split(",");
for(i=0;i<b.length;i++)
alert(b[i].substring(b[i].lastIndexOf("[")+1,b[i].lastIndexOf("]")))
答案 1 :(得分:0)
你不需要jQuery,你可以简单地使用纯JavaScript并使用String操作或正则表达式。使用正则表达式,您可以执行以下操作:
var data = "response.endpoints[0].., response.endpoints[1].., response.endpoints[2]..";
var regex = /response\.endpoints\[(\d+)\]/g;
var match;
while (match = regex.exec(data)) {
var number = match[1];
console.log(number);
}
这将打印上述数据的数字0到2。如果您对附加信息(..
部分)感兴趣,则必须扩展正则表达式。