Javascript create string with new lines from csv file
Im trying to load csv file in React and then format it to be saved as a string with line breaks. The problem is this:
After this code block:
makeArrayString() {
let array = this.state.tempData;
let finalArray = ;
array.forEach(function(element) {
let joinedEl = element.join(",");
finalArray.push(joinedEl + "n");
});
The console output looks like this:
0: "5.1,3.5,1.4,0.2,setosa↵"
1: "4.9,3,1.4,0.2,setosa↵"
2: "4.7,3.2,1.3,0.2,setosa↵"
3: "4.6,3.1,1.5,0.2,setosa↵"
4: "5,3.6,1.4,0.2,setosa↵"
I cant send this kind of format to python script on server because of the ↵ char. However when this block of code gets executed:
finalArray = finalArray + "";
finalArray.split("n").map((item, key) => {
return (
<React.Fragment key={key}>
{item}
<br />
</React.Fragment>
);
});
console.log(finalArray);
The data now looks like this:
5.1,3.5,1.4,0.2,setosa
,4.9,3,1.4,0.2,setosa
,4.7,3.2,1.3,0.2,setosa
,4.6,3.1,1.5,0.2,setosa
,5,3.6,1.4,0.2,setosa
Which is just a string thats needed, only those commas at line beginnings stand in the way. The dataset is now one huge string and I cant find a way to remove those commas at line beginnings. Is there a way to get good format by changing any of those steps?
Also Im using Papa Parse for reading the csv file with the block:
loadCsv() {
Papa.parse(Iris, {
header: false,
download: true,
dynamicTyping: true,
complete: results => {
this.setState({ tempData: results.data });
//console.log(this.state.tempData);
this.makeArrayString();
}
});
}
javascript reactjs papaparse
add a comment |
Im trying to load csv file in React and then format it to be saved as a string with line breaks. The problem is this:
After this code block:
makeArrayString() {
let array = this.state.tempData;
let finalArray = ;
array.forEach(function(element) {
let joinedEl = element.join(",");
finalArray.push(joinedEl + "n");
});
The console output looks like this:
0: "5.1,3.5,1.4,0.2,setosa↵"
1: "4.9,3,1.4,0.2,setosa↵"
2: "4.7,3.2,1.3,0.2,setosa↵"
3: "4.6,3.1,1.5,0.2,setosa↵"
4: "5,3.6,1.4,0.2,setosa↵"
I cant send this kind of format to python script on server because of the ↵ char. However when this block of code gets executed:
finalArray = finalArray + "";
finalArray.split("n").map((item, key) => {
return (
<React.Fragment key={key}>
{item}
<br />
</React.Fragment>
);
});
console.log(finalArray);
The data now looks like this:
5.1,3.5,1.4,0.2,setosa
,4.9,3,1.4,0.2,setosa
,4.7,3.2,1.3,0.2,setosa
,4.6,3.1,1.5,0.2,setosa
,5,3.6,1.4,0.2,setosa
Which is just a string thats needed, only those commas at line beginnings stand in the way. The dataset is now one huge string and I cant find a way to remove those commas at line beginnings. Is there a way to get good format by changing any of those steps?
Also Im using Papa Parse for reading the csv file with the block:
loadCsv() {
Papa.parse(Iris, {
header: false,
download: true,
dynamicTyping: true,
complete: results => {
this.setState({ tempData: results.data });
//console.log(this.state.tempData);
this.makeArrayString();
}
});
}
javascript reactjs papaparse
1
↵
is just the way the console is choosing to represent the newline character. How are you sending it to your server? Depending on whether or not you need to escape it, It should just be a "n", shouldn't it?
– Khauri McClain
Nov 22 at 20:09
And leading comma is probably coming from here:finalArray = finalArray + "";
. Which is the same asfinalArray.join(",") + ""
. You should usefinalArray.join("")
instead.
– Khauri McClain
Nov 22 at 20:12
The linefinalArray = finalArray + ""
is needed for the.split()
function on the next line to work, if I comment it then an error occurs. The extra comma probably happens because of the newline character. Thank you for suggestions, I tried changing the.join("")
but the output is still not apropriate.
– lafro
Nov 22 at 23:46
No, don't comment it out, change it tofinalArray.join("")
. It will work.finalArray + ""
is just a (tbh sort of lazy) way of convertingfinalArray
to a string. It's equivalent tofinalArray.toString() + ""
andfinalArray.toString()
joins the elements of the array with a comma, hence why you get an extra comma after your newline.
– Khauri McClain
Nov 23 at 13:53
add a comment |
Im trying to load csv file in React and then format it to be saved as a string with line breaks. The problem is this:
After this code block:
makeArrayString() {
let array = this.state.tempData;
let finalArray = ;
array.forEach(function(element) {
let joinedEl = element.join(",");
finalArray.push(joinedEl + "n");
});
The console output looks like this:
0: "5.1,3.5,1.4,0.2,setosa↵"
1: "4.9,3,1.4,0.2,setosa↵"
2: "4.7,3.2,1.3,0.2,setosa↵"
3: "4.6,3.1,1.5,0.2,setosa↵"
4: "5,3.6,1.4,0.2,setosa↵"
I cant send this kind of format to python script on server because of the ↵ char. However when this block of code gets executed:
finalArray = finalArray + "";
finalArray.split("n").map((item, key) => {
return (
<React.Fragment key={key}>
{item}
<br />
</React.Fragment>
);
});
console.log(finalArray);
The data now looks like this:
5.1,3.5,1.4,0.2,setosa
,4.9,3,1.4,0.2,setosa
,4.7,3.2,1.3,0.2,setosa
,4.6,3.1,1.5,0.2,setosa
,5,3.6,1.4,0.2,setosa
Which is just a string thats needed, only those commas at line beginnings stand in the way. The dataset is now one huge string and I cant find a way to remove those commas at line beginnings. Is there a way to get good format by changing any of those steps?
Also Im using Papa Parse for reading the csv file with the block:
loadCsv() {
Papa.parse(Iris, {
header: false,
download: true,
dynamicTyping: true,
complete: results => {
this.setState({ tempData: results.data });
//console.log(this.state.tempData);
this.makeArrayString();
}
});
}
javascript reactjs papaparse
Im trying to load csv file in React and then format it to be saved as a string with line breaks. The problem is this:
After this code block:
makeArrayString() {
let array = this.state.tempData;
let finalArray = ;
array.forEach(function(element) {
let joinedEl = element.join(",");
finalArray.push(joinedEl + "n");
});
The console output looks like this:
0: "5.1,3.5,1.4,0.2,setosa↵"
1: "4.9,3,1.4,0.2,setosa↵"
2: "4.7,3.2,1.3,0.2,setosa↵"
3: "4.6,3.1,1.5,0.2,setosa↵"
4: "5,3.6,1.4,0.2,setosa↵"
I cant send this kind of format to python script on server because of the ↵ char. However when this block of code gets executed:
finalArray = finalArray + "";
finalArray.split("n").map((item, key) => {
return (
<React.Fragment key={key}>
{item}
<br />
</React.Fragment>
);
});
console.log(finalArray);
The data now looks like this:
5.1,3.5,1.4,0.2,setosa
,4.9,3,1.4,0.2,setosa
,4.7,3.2,1.3,0.2,setosa
,4.6,3.1,1.5,0.2,setosa
,5,3.6,1.4,0.2,setosa
Which is just a string thats needed, only those commas at line beginnings stand in the way. The dataset is now one huge string and I cant find a way to remove those commas at line beginnings. Is there a way to get good format by changing any of those steps?
Also Im using Papa Parse for reading the csv file with the block:
loadCsv() {
Papa.parse(Iris, {
header: false,
download: true,
dynamicTyping: true,
complete: results => {
this.setState({ tempData: results.data });
//console.log(this.state.tempData);
this.makeArrayString();
}
});
}
javascript reactjs papaparse
javascript reactjs papaparse
asked Nov 22 at 20:01
lafro
183
183
1
↵
is just the way the console is choosing to represent the newline character. How are you sending it to your server? Depending on whether or not you need to escape it, It should just be a "n", shouldn't it?
– Khauri McClain
Nov 22 at 20:09
And leading comma is probably coming from here:finalArray = finalArray + "";
. Which is the same asfinalArray.join(",") + ""
. You should usefinalArray.join("")
instead.
– Khauri McClain
Nov 22 at 20:12
The linefinalArray = finalArray + ""
is needed for the.split()
function on the next line to work, if I comment it then an error occurs. The extra comma probably happens because of the newline character. Thank you for suggestions, I tried changing the.join("")
but the output is still not apropriate.
– lafro
Nov 22 at 23:46
No, don't comment it out, change it tofinalArray.join("")
. It will work.finalArray + ""
is just a (tbh sort of lazy) way of convertingfinalArray
to a string. It's equivalent tofinalArray.toString() + ""
andfinalArray.toString()
joins the elements of the array with a comma, hence why you get an extra comma after your newline.
– Khauri McClain
Nov 23 at 13:53
add a comment |
1
↵
is just the way the console is choosing to represent the newline character. How are you sending it to your server? Depending on whether or not you need to escape it, It should just be a "n", shouldn't it?
– Khauri McClain
Nov 22 at 20:09
And leading comma is probably coming from here:finalArray = finalArray + "";
. Which is the same asfinalArray.join(",") + ""
. You should usefinalArray.join("")
instead.
– Khauri McClain
Nov 22 at 20:12
The linefinalArray = finalArray + ""
is needed for the.split()
function on the next line to work, if I comment it then an error occurs. The extra comma probably happens because of the newline character. Thank you for suggestions, I tried changing the.join("")
but the output is still not apropriate.
– lafro
Nov 22 at 23:46
No, don't comment it out, change it tofinalArray.join("")
. It will work.finalArray + ""
is just a (tbh sort of lazy) way of convertingfinalArray
to a string. It's equivalent tofinalArray.toString() + ""
andfinalArray.toString()
joins the elements of the array with a comma, hence why you get an extra comma after your newline.
– Khauri McClain
Nov 23 at 13:53
1
1
↵
is just the way the console is choosing to represent the newline character. How are you sending it to your server? Depending on whether or not you need to escape it, It should just be a "n", shouldn't it?– Khauri McClain
Nov 22 at 20:09
↵
is just the way the console is choosing to represent the newline character. How are you sending it to your server? Depending on whether or not you need to escape it, It should just be a "n", shouldn't it?– Khauri McClain
Nov 22 at 20:09
And leading comma is probably coming from here:
finalArray = finalArray + "";
. Which is the same as finalArray.join(",") + ""
. You should use finalArray.join("")
instead.– Khauri McClain
Nov 22 at 20:12
And leading comma is probably coming from here:
finalArray = finalArray + "";
. Which is the same as finalArray.join(",") + ""
. You should use finalArray.join("")
instead.– Khauri McClain
Nov 22 at 20:12
The line
finalArray = finalArray + ""
is needed for the .split()
function on the next line to work, if I comment it then an error occurs. The extra comma probably happens because of the newline character. Thank you for suggestions, I tried changing the .join("")
but the output is still not apropriate.– lafro
Nov 22 at 23:46
The line
finalArray = finalArray + ""
is needed for the .split()
function on the next line to work, if I comment it then an error occurs. The extra comma probably happens because of the newline character. Thank you for suggestions, I tried changing the .join("")
but the output is still not apropriate.– lafro
Nov 22 at 23:46
No, don't comment it out, change it to
finalArray.join("")
. It will work. finalArray + ""
is just a (tbh sort of lazy) way of converting finalArray
to a string. It's equivalent to finalArray.toString() + ""
and finalArray.toString()
joins the elements of the array with a comma, hence why you get an extra comma after your newline.– Khauri McClain
Nov 23 at 13:53
No, don't comment it out, change it to
finalArray.join("")
. It will work. finalArray + ""
is just a (tbh sort of lazy) way of converting finalArray
to a string. It's equivalent to finalArray.toString() + ""
and finalArray.toString()
joins the elements of the array with a comma, hence why you get an extra comma after your newline.– Khauri McClain
Nov 23 at 13:53
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437414%2fjavascript-create-string-with-new-lines-from-csv-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437414%2fjavascript-create-string-with-new-lines-from-csv-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
↵
is just the way the console is choosing to represent the newline character. How are you sending it to your server? Depending on whether or not you need to escape it, It should just be a "n", shouldn't it?– Khauri McClain
Nov 22 at 20:09
And leading comma is probably coming from here:
finalArray = finalArray + "";
. Which is the same asfinalArray.join(",") + ""
. You should usefinalArray.join("")
instead.– Khauri McClain
Nov 22 at 20:12
The line
finalArray = finalArray + ""
is needed for the.split()
function on the next line to work, if I comment it then an error occurs. The extra comma probably happens because of the newline character. Thank you for suggestions, I tried changing the.join("")
but the output is still not apropriate.– lafro
Nov 22 at 23:46
No, don't comment it out, change it to
finalArray.join("")
. It will work.finalArray + ""
is just a (tbh sort of lazy) way of convertingfinalArray
to a string. It's equivalent tofinalArray.toString() + ""
andfinalArray.toString()
joins the elements of the array with a comma, hence why you get an extra comma after your newline.– Khauri McClain
Nov 23 at 13:53