1

我有以下代码(请耐心等待,我根本不是 Javascript 程序员)

我已经能够加载相关的文本文件,但无法将它们转换为数组,有人可以解释一下吗?

function Login(form) {

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    USRFile = fso.OpenTextFile("C:\\Users.txt", 1, false, 0);
    var USR_LIST = USRFile.Read(1000)
var USRitems = USR_LIST.split(",");
    USRFile.Close();
    fso = null;

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    PWDFile = fso.OpenTextFile("C:\\passwords.txt", 1, false, 0);
    var PWD_LIST = PWDFile.Read(1000)
var PWDitems = PWD_LIST.split(",");
    PWDFile.Close();
    fso = null;

username = new Array(USRitems);
password = new Array(PWDitems);
page = "SETTINGS.html";
if (form.username.value == username[0] && form.password.value == password[0] || form.username.value == username[1] && form.password.value == password[1] || form.username.value == username[2] && form.password.value == password[2] || form.username.value == username[3] && form.password.value == password[3] || form.username.value == username[4] && form.password.value == password[4] || form.username.value == username[5] && form.password.value == password[5] || form.username.value == username[6] && form.password.value == password[6] || form.username.value == username[7] && form.password.value == password[7] || form.username.value == username[8] && form.password.value == password[8] || form.username.value == username[9] && form.password.value == password[9]) {
self.location.href = page;
}
else {
page = "Access_Violation.html";
self.location.href = page;
form.username.focus();
}
return true;
}

我在文本 Password.txt 文件中有以下内容

"p1","p2","p3"

对于 User.txt 我有

"u1","u2","u3"

我只是无法获得数组???

4

3 回答 3

1

split返回一个数组。当你这样做时:

username = new Array(USRitems);

您正在将已有的数组放入一个新数组中。

你只是想做:

username = USRItems

例子:

var array = [1,2,3];
console.log(array);
var array2 = new Array(array);
console.log(array2);

输出:

[1, 2, 3]
[[1, 2, 3]] // note the extra brackets
于 2013-11-12T12:32:00.100 回答
0
var array = string.split(',',2);
于 2013-11-12T12:34:54.340 回答
0

你已经有两个数组形式

var USRitems = USR_LIST.split(",");

var PWDitems = PWD_LIST.split(",");

是具有值的数组。split函数就是这样做的。这是它的工作原理:

string.split([separator][, limit])

参数

分隔符 指定用于分隔字符串的字符。分隔符被视为字符串或正则表达式。如果省略分隔符,则返回的数组包含一个由整个字符串组成的元素。如果 separator 为空字符串,则将字符串转换为字符数组。

limit 整数,指定要找到的拆分数量的限制。split 方法仍然在分隔符的每个匹配项上进行拆分,但它会将返回的数组截断为最多限制元素。

您可以在MDN上阅读更多内容

于 2013-11-12T12:36:07.127 回答