Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Text File to JavaScript Nested Array

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

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

}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!