2

I have this .txt file to change to a nested array:

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

to the format:

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]
    ]

what I have done so far is:

  1. Read the file:
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)
    }
}

and 2. Split the File

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

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

I know I need to push through a for loop, but not sure how to get the nested array.

2 Answers 2

4

Split the string by a newline, then map over each item and convert the string into an array of characters with 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)

To convert the characters to numbers, map over the array of characters and parse each item:

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)

Sign up to request clarification or add additional context in comments.

7 Comments

This is close, but the result needed is not a nested array of characters, it's a nesterd array of numbers.
This is what I am looking for. Just on error it does is that it adds an additional element to each array except the last.
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 Are you sure your input data is correct? Can't reproduce that behavior with the code in my answer.
confirming data is the same as in original mention
|
0

You turn matrix into a a string of ones and zeros, you just need to split the string and convert them to numbers

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

}

1 Comment

This is a good solution but it also gives an additional element in the first 11 arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.