0

I want to concatenate two String-Arrays which are loaded by the fetch-API. For now i am just trying to print the first Array, but the forEach-method resolves with a TypeError. If you have any advice on how to fix this and possibly implement concatenation in a good way, I'd be very grateful.

I haven't found an explanation as to why the function isn't recognized as such.

<body>
<p id="content"></p>

<script>


    let content = "";
    document.getElementById("content").innerHTML = content;

    function stringConcat(item, index) {
        content += item + " " + "<br>"
    }

    async function fetchText() {
        const p1 = await fetch("A.txt")
            .then(response => response.text())            // return the Input as a String
            .then(response => response.split("\r\n"))     // Split response into an Array as Substrings
            //.then(textField => console.log(textField))
            .catch(error => console.error("Request failed ", error));
        console.log(p1);

        const p2 = await fetch("B.txt")
            .then(response => response.text())
            .then(response => response.split("\r\n"))
            .catch(error => console.error("Request failed ", error));
        console.log(p2);

        p1.forEach(stringConcat());
    }
    fetchText();

</script>
</body>
VLAZ
  • 26,331
  • 9
  • 49
  • 67

0 Answers0