Upload CSV file with dynamic fields in angular

I have this format csv file

Name | Email | Contact | Designation | Address

where Name, Email and contact is fixed field but Designation or address or any other fields user can add in csv file while uploading

Code to upload CSV

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>async uploadCSV(ev){
const reader = new FileReader();
const file = ev.target.files[0];
var file_name = file.name.split(".");
var file_ext = file_name[file_name.length - 1].toLowerCase();
if (file_ext !== "csv") {
return this.toaster.error("Please upload csv file");
} else {
var output;
reader.onload = () => {
var csvToText = reader.result;
output = this.csvToJSON(csvToText);
};
reader.readAsText(file);
await new Promise((f) => setTimeout(f, 2000));
}
}
dataSource = [];
csvToJSON(csv) {
var lines = csv.split("n");
var result = [];
var headers;
var fileType;
headers = lines[0].split(",");
headers = headers.map((s) => s.replace(/n|t|r/gm, ""), headers);
for (var i = 1; i < lines.length; i++) {
console.log("lines ---", lines);
let obj = {};
if (lines[i] == undefined || lines[i].trim() == "") {
continue;
}
var words = lines[i].split(",");
for (var j = 0; j < words.length; j++) {
let name;
let lname;
let userEmail;
let userPhone='-';
let nameIndex = headers.indexOf("Name");
let contactIndex = headers.indexOf("Contact");
let emailIndex= headers.indexOf("Email");
name=nameIndex != -1 ? words[nameIndex].replace(/n|t|r/gm, "") : "";
userEmail=emailIndex != -1 ? words[emailIndex].replace(/n|t|r/gm, "") : "";
userPhone=phoneIndex != -1 ? words[contactIndex].replace(/n|t|r/gm, "") : "";
console.log('words length ----> ');
console.log('words',words);
/* End */
obj = {
Name: name,
Email:userEmail,
MobileNumber: userPhone,
jsonData: '',
};
}
result.push(obj);
}
this.dataSource = result;
console.log("datadource ", this.dataSource);
}
</code>
<code>async uploadCSV(ev){ const reader = new FileReader(); const file = ev.target.files[0]; var file_name = file.name.split("."); var file_ext = file_name[file_name.length - 1].toLowerCase(); if (file_ext !== "csv") { return this.toaster.error("Please upload csv file"); } else { var output; reader.onload = () => { var csvToText = reader.result; output = this.csvToJSON(csvToText); }; reader.readAsText(file); await new Promise((f) => setTimeout(f, 2000)); } } dataSource = []; csvToJSON(csv) { var lines = csv.split("n"); var result = []; var headers; var fileType; headers = lines[0].split(","); headers = headers.map((s) => s.replace(/n|t|r/gm, ""), headers); for (var i = 1; i < lines.length; i++) { console.log("lines ---", lines); let obj = {}; if (lines[i] == undefined || lines[i].trim() == "") { continue; } var words = lines[i].split(","); for (var j = 0; j < words.length; j++) { let name; let lname; let userEmail; let userPhone='-'; let nameIndex = headers.indexOf("Name"); let contactIndex = headers.indexOf("Contact"); let emailIndex= headers.indexOf("Email"); name=nameIndex != -1 ? words[nameIndex].replace(/n|t|r/gm, "") : ""; userEmail=emailIndex != -1 ? words[emailIndex].replace(/n|t|r/gm, "") : ""; userPhone=phoneIndex != -1 ? words[contactIndex].replace(/n|t|r/gm, "") : ""; console.log('words length ----> '); console.log('words',words); /* End */ obj = { Name: name, Email:userEmail, MobileNumber: userPhone, jsonData: '', }; } result.push(obj); } this.dataSource = result; console.log("datadource ", this.dataSource); } </code>
async uploadCSV(ev){
  const reader = new FileReader();
  const file = ev.target.files[0];
  var file_name = file.name.split(".");
  var file_ext = file_name[file_name.length - 1].toLowerCase();
  if (file_ext !== "csv") {
    return this.toaster.error("Please upload csv file");
  } else {
    var output;
    reader.onload = () => {
      var csvToText = reader.result;
      output = this.csvToJSON(csvToText);
    };
    reader.readAsText(file);
    await new Promise((f) => setTimeout(f, 2000));
  }
}


dataSource = [];
csvToJSON(csv) {
  var lines = csv.split("n");
  var result = [];
  var headers;
  var fileType;
  headers = lines[0].split(",");
  headers = headers.map((s) => s.replace(/n|t|r/gm, ""), headers);
  for (var i = 1; i < lines.length; i++) {
    console.log("lines ---", lines);
    let obj = {};
    if (lines[i] == undefined || lines[i].trim() == "") {
      continue;
    }
    var words = lines[i].split(",");
    for (var j = 0; j < words.length; j++) {

   
      let name;
      let lname;
      let userEmail;
      let userPhone='-';
      let nameIndex = headers.indexOf("Name");
      let contactIndex = headers.indexOf("Contact");
      let emailIndex= headers.indexOf("Email");

      name=nameIndex != -1 ? words[nameIndex].replace(/n|t|r/gm, "") : "";
      userEmail=emailIndex != -1 ? words[emailIndex].replace(/n|t|r/gm, "") : "";
      userPhone=phoneIndex != -1 ? words[contactIndex].replace(/n|t|r/gm, "") : "";

      console.log('words length ----> ');
      console.log('words',words);
      
      /* End */





      obj = {
        Name: name,
        Email:userEmail,
        MobileNumber: userPhone,
        jsonData: '',
      };
    }
    result.push(obj);
  }
  this.dataSource = result;
  console.log("datadource ", this.dataSource);
}

i’m fetching these three fixed fields but i want to fetch other dynamic fields and it’s values like if it is a designation then

{designation : ‘Manager’,address:’UK’,…}

and pass it to json data

any solution Thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật