1

I have a txt file on mydomain.net/file.txt.How can I read this into an array filecontent where each new line is a element and ignore lines containing //? .

JavaScript

    var filecontent=[];
    function txtToArray(link){
    //code here
    }
    txtToArray("mydomain.net/file.txt");
document.write(filecontent);

mydomain.net/file.txt

//file.txt
    aaa
    aa1
    aa2

1 Answer 1

1

If you're in the browser, you'll have to fetch the file, parse the response as text, split the text into an array of lines, and filter the array for lines that do not start with //, then write the filtered array:

function filterAndWrite(text) {
    const arrayOfLines = text.split('\n');
    const startsWithDoubleSlash = new RegExp('^//');
    const filteredLines = arrayOfLines.filter((line) => !line.match(startsWithDoubleSlash));
    document.write(filteredLines);
}

function writeToDocument(link) {
    fetch(link)
        .then((response) => response.text())
        .then(filterAndWrite);
}

writeToDocument('mydomain.net/file.txt');
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.