【问题标题】:Text File to JavaScript Nested Array文本文件到 JavaScript 嵌套数组
【发布时间】:2022-01-04 18:07:31
【问题描述】:

我有这个 .txt 文件要更改为嵌套数组:

000011000000
000100001100
000001100001
010010001000
100101000100
101010010001
001000001001
000001000111
010100100010
010010010010
000000011100
001001110000

转格式:

var data = [
        [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0],
        [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1],
        [0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
        [1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0],
        [1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1],
        [0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1],
        [0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
        [0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0]
    ]

到目前为止我所做的是:

  1. 读取文件:
function readMatrixFile(){
    var inputElement = document.getElementById("adjencyMatrixInput");
    var fileList = inputElement.files;
    var plainMatrix = new FileReader();
    plainMatrix.readAsText(fileList[0]);
    plainMatrix.onload = function () {
        //Add to Matrix
        renderMatrix(plainMatrix)
    }
}

和 2. 拆分文件

function renderMatrix(plainMatrix) {
    var matrix = plainMatrix.result;
    var mtx = [];

    matrix = matrix.split("\n");
    
}

我知道我需要通过 for 循环,但不确定如何获取嵌套数组。

【问题讨论】:

    标签: javascript arrays multidimensional-array


    【解决方案1】:

    你把matrix变成一串1和0,你只需要把字符串拆分成数字就行了

    function renderMatrix(plainMatrix) {
        var matrix = plainMatrix.result;
    
        var mtx = matrix.split("\n"); // mtx is now an array of strings of ones and zeros
        
        mtx = mtx.map(string => string.split('')); // mtx is now an array of arrays of number string
    
        mtx = mtx.map(nested => nested.map(numberString => Number(numberString))); // mtx is now what you want
    
    }
    

    【讨论】:

    • 这是一个很好的解决方案,但它也在前 11 个数组中提供了一个额外的元素。
    【解决方案2】:

    Split 字符串由换行符,然后map 覆盖每个项目,并将字符串转换为带有spread syntax 的字符数组。

    const str = `000011000000
    000100001100
    000001100001
    010010001000
    100101000100
    101010010001
    001000001001
    000001000111
    010100100010
    010010010010
    000000011100
    001001110000`
    
    const res = str.split("\n").map(e => [...e])
    console.log(res)

    要将字符转换为数字,请映射字符数组并解析每个项目:

    const str = `000011000000
    000100001100
    000001100001
    010010001000
    100101000100
    101010010001
    001000001001
    000001000111
    010100100010
    010010010010
    000000011100
    001001110000`
    
    const res = str.split("\n").map(e => [...e].map(e => +e))
    console.log(res)

    【讨论】:

    • 这很接近,但需要的结果不是嵌套的字符数组,而是嵌套的数字数组。
    • 这就是我要找的。只是出错了,它为每个数组添加了一个额外的元素,除了最后一个。
    • 0: (13) [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0] 1: (13) [0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0] 2: (13) [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0] 3: (13) [0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0] 4: (13) [1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0] 5: (13) [1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0] 6: (13) [0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0] 7: (13) [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0] 8: (13) [0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0] 9: (13) [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0] 10: (13) [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0] 11: (12) [0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0]
    • @ELMERRONIERHUITZ 您确定您的输入数据正确吗?无法使用我的答案中的代码重现该行为。
    • 确认数据与原提相同
    猜你喜欢
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多