TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sujeet Raman
857
927
357.4k
how to convert csv file to json format in angular 4?
Nov 8 2017 8:09 AM
i need to select one csv file and convert it in to json list.for that i wrote a javascript function but its not giving the exat json list
this is the js method
<input type=
"file"
accept=
".csv"
(change)=
"convertFile($event)"
/>
convertFile(csv: any) {
this
.fileReaded = csv.target.files[0];
let reader: FileReader =
new
FileReader();
reader.readAsText(
this
.fileReaded);
reader.onload = (e) => {
let csv: string = reader.result;
let allTextLines = csv.split(/\r|\n|\r/);
let headers = allTextLines[0].split(
','
);
let lines = [];
for
(let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(
','
);
if
(data.length === headers.length) {
let tarr = [];
for
(let j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
// log each row to see output
console.log(tarr);
lines.push(tarr);
}
}
// all rows in the csv file
console.log(
">>>>>>>>>>>>>>>>>"
, lines);
}
but this is giving o/p like
. Array(21)
. 0:(3) ["ONE", "TWO", "THREE"]
. 1:(3) ["9345", "ui: Publishing Start Date and end date should add calendar function", "Committed"]
. 2:(3) ["9339", "gg: The options for default shall be in language table as Y and N", "Approved"]
. 3:(3) ["9295", "URAXR2: A message info is to be added after delete the variable in variable tab ", "bb"]
. 4:(3) ["9276", "bb: Valid error message should be added in Formula tab", "Approved"]
. 5:(3) ["9275", "bb: The spelling of the record is incorrect in…ssage info when deleting record from nntables", "Approved"]
. 6:(3) ["9274", "nn:? The values get blank when edit the record in Group tables tab", "Committed"]
. 7:(3) [
i need to get like below
[
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
},
{
color: "cyan",
value: "#0ff"
},
]
Reply
Answers (
1
)
How to use tool tip in prime ng dynamic data table ?
update and add new row is not working in angular 4