+ 3

ERROR: Uncuaght TypeError: ar.splice is not a function

/* I have written the following code in JavaScript The code opens a page where I can add a .txt-file Then the code looks trough the program to find a linebreak ('\n') When it findes this linebreak I would like the code to seperate this array and print it in the console, and then look for the next linebreak and repeat But currently I am having problems with the ERROR in the quistion name (Uncaught TypeError: ar.splice is not a function) I hope someone can help me with this and tank you for your time */ function fileReader(event) { var input = event.target; var reader = new FileReader(); reader.onload = function() { var text = reader.result; ar = reader.result.substring(0, 1377); console.log(ar); for(var i = 0; i < ar.length; i++) { console.log(ar[i]); if(ar[i] == '\n') { ar = ar.splice(1, ar.indexOf('\n')); console.log(ar); } } } reader.readAsText(input.files[0]); }

22nd Oct 2018, 2:20 PM
Sem Pepels
Sem Pepels - avatar
2 Antworten
+ 5
Odd, this is working for me if I strip it down to: ar = "somestring".substring(...); alert(typeof ar) // string, no splice() error? Perhaps you could check that reader.result is the expected type (like, not null or undefined or something)?
22nd Oct 2018, 4:09 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
.splice() method is applicable only to arrays. Variable ar appears to store a string instead of an array. Perhaps you meant to use .slice() ?
22nd Oct 2018, 2:27 PM
Hatsy Rei
Hatsy Rei - avatar