-1

I want to know how i could read a txt file and have the contents of it be into array in javascript.

My txt file looks something like this. [Skin1],[skin2],[skin3],etc...

I simply want the array contents to be acquired from the txt file instead of writing the array into the actual html file.

I am fairly new to javascript and do not understand how to do this.

P.s. my html and txt files will be on the same server. If that makes a difference.

I would really appreciate an example code, thanks in advance.

9
  • 1
    Your text file's syntax is not valid JSON. I'd suggest fixing whatever produces the text file to save valid JSON instead, and then you could use JSON.parse to turn its text into an array immediately. Commented Oct 2, 2018 at 23:15
  • 2
    Possible duplicate of How can I parse a text file using javascript Commented Oct 2, 2018 at 23:16
  • 2
    @CertainPerformance there's nothing in the question that has anything to do with JSON. Commented Oct 2, 2018 at 23:18
  • 2
    @DanielBeck No, but if the text is changed so that it is JSON, it'll be far easier for OP to work with it. Commented Oct 2, 2018 at 23:19
  • 2
    I am looking for guidance, i really dont know how to read a file that is my main question. If someone could provide an example of how to use the json way i would appreciate it. Thank you for quick responses Commented Oct 2, 2018 at 23:32

1 Answer 1

0

As stated in the comments, this would be more efficiently achieved if the text file was encoded in JSON. However without knowing how the text file is being produced I can't recommend a method to generate it as JSON, so here's how you might parse your current file.

Assuming you're are reading the content of the text file in the frontend, you would asynchronously get the content of the text file using XMLHttpRequest. Once you've received the file content you would turn it into an array. In the specific case that you've provided where the text file is of the form [Skin1],[skin2],[skin3], you would remove the first [ and last ], and then split by the delineation ],[.

function httpGet(url, callback) {
    // this function gets the contents of the URL. Once the
    // content is present it runs the callback function.
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", url, false );
    xmlhttp.send();    
}

httpGet("url-of-your-file", function(textFile){
    // this calls the httpGet function with the URL of your text
    // file. It then runs a function that turns the file into an
    // array.
    var array = textFile.slice(1, -1).split("],[");
    console.log(array);
});
Sign up to request clarification or add additional context in comments.

Comments

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.