2

I'm trying to use JS to:

  1. Read several .txt files which contain JSON objects as text
  2. Save these text strings to an array (with length equal to the number of files I'm reading in)
  3. Write essentially a long comma-separated list of all the values in rows[0].elements[i].distance.text in all the JSON objects.

My code looks like this:

<!DOCTYPE html>
<html>
<body>

<h2>Distances</h2>

<script>
    
    document.write("Test string!")
    
    var fr = new FileReader();

    var files = ['Abing0.txt', 'Abing1.txt', 'Abing2.txt', 'Abing3.txt', 'Abing4.txt', 'Abing5.txt', 'Abing6.txt', 'Abing7.txt', 'Abing8.txt', 'Abing9.txt', 'Abing10.txt', 'Abing11.txt', 'Abing12.txt', 'Abing13.txt', 'Abing14.txt'];

    for (int p = 0; p < files.length; p++){
        files[p] = new FileReader().readAsText(files[p]);
        files[p] = JSON.parse(files[p]);
    }
    
    var myAddress = [];

    for (var l = 0; l < files.length < l++){
        for (var i = 0; i < 24; i++) {
            myAddress[i] = files[l].rows[0].elements[i].distance.text;
            document.write(myAddress[i] + ", ");
        }
    }
</script>
</body>
</html>

This is the content of Abing6.txt, as an example.

I suspect that something in my first for loop is causing the issues as I have little experience with FileReader or JSON, but any help would be appreciated.

  • Could you try to run the code present in `script` tag in a node.js environment. It looks like it is a node.js code and not browserside code. – Siddhesh Kulkarni Jan 18 '21 at 20:48

0 Answers0