I run the following
let res = e.target.result.split('\n').map(line => line.split(','));
var myData = $("#dvCSV").text(JSON.stringify(res, null, ' '));
myData = myData.replace("\r", " ");
$("#dvCSV").append(myData);
But I still get \r in the output
[
[
"New York",
"Employee",
"20\r"
],
[
"Singapore",
"Employee",
"15"
]
I run the following
let res = e.target.result.split('\n').map(line => line.split(','));
var myData = $("#dvCSV").text(JSON.stringify(res, null, ' '));
myData = myData.replace("\r", " ");
$("#dvCSV").append(myData);
But I still get \r in the output
[
[
"New York",
"Employee",
"20\r"
],
[
"Singapore",
"Employee",
"15"
]
Share
Improve this question
edited Mar 31, 2018 at 14:05
rob.m
asked Mar 31, 2018 at 13:52
rob.mrob.m
10.6k21 gold badges88 silver badges175 bronze badges
3
- You're using both "\r" and "/r" in your question, "\r" has special meaning (escape for a return character) which one do you really mean? It appears you're trying to remove the "\r" strings from the JSON output but it's not clear from your question. – ellipse-of-uncertainty Commented Mar 31, 2018 at 14:03
-
split('\r\n')
in first line – chuck911 Commented Mar 31, 2018 at 14:05 - @hyphenthis yup, updated thanks. – rob.m Commented Mar 31, 2018 at 14:05
3 Answers
Reset to default 4You need to escape the backslash, and do the replace at the stringify
, where it is a string
JSON.stringify(res, null, ' ').replace("\\r", " ");
Stack snippet sample
var res = [
[
"New York",
"Employee",
"20\r"
],
[
"Singapore",
"Employee",
"15"
]
];
console.log(JSON.stringify(res, null, ' ').replace("\\r", " ") )
replace method only removes first instance. in your case, only first instance of "\r"
Use JSON.parse and stringify together, like
JSON.parse(JSON.stringify(res));