通过 AJAX 从文件中读取的字符串索引没有任何意义


Index Of String read from file via AJAX makes no sense whatsoever

>我用AJAX读取了一个文件。返回值存储在字符串中。话虽如此,我需要对其进行一些文本解析。我决定创建一个小代码块,输出与其索引值链接的字符串的值。结果完全没有意义。我也确信显示这一点的代码也是正确的。

Javascript:

    function removeFromFile(command, file){
        if (command.length <= 4){
            var api = command[3];
            $.ajax({
                url: 'process.php?fName=' + file,
                type: 'POST',
                datatype: "html",
                cache: false,
                async: false,
                data: {displayFile : true},
                success: function (r) {
                    output(r);
                    for (var i =0; i < r.length; i++){
                        output(r[i] + "    " + r.indexOf(r[i]));
                    }
                },
                error: function (response) {
                    alert('Something went wrong in the update! Ref: ');
                }
            });
        }
    }

txt 文件如下所示:

    123-12333 : Duan Uys 
    345-34555 : Dennis Taylor 

输出如下所示: 耦合方式如下:值/index_value

值/index_value根本没有意义?

    123-12333 : Duan Uys 
    345-34555 : Dennis Taylor 
    1    0
    2    1
    3    2
    -    3
    1    0
    2    1
    3    2
    3    2
    3    2
         9
    :    10
         9
    D    12
    u    13
    a    14
    n    15
         9
    U    17
    y    18
    s    19
         9
        21
        21
    3    2
    4    24
    5    25
    -    3
    3    2
    4    24
    5    25
    5    25
    5    25
         9
    :    10
         9
    D    12
    e    36
    n    15
    n    15
    i    39
    s    19
         9
    T    42
    a    14
    y    18
    l    45
    o    46
    r    47

indexOf返回与您要搜索的内容匹配的第一个索引。因此,每次在字符串中遇到"1"时,它都会返回 0,因为这是字符串中包含"1"的第一个索引。

反正也没必要用indexOf。只需使用i.它表示您记录到控制台的字符的当前索引。

output(r[i] + "    " + i);