“Buat file dari array simpan js” Kode Jawaban

Buat file dari array simpan js

async function saveCSV () {
  // (A) ARRAY OF DATA
  var array = [
    ["Job", "[email protected]", "123456"],
    ["Joe", "[email protected]", "234567"],
    ["Joi", "[email protected]", "345678"],
    ["Jon", "[email protected]", "456789"],
    ["Jou", "[email protected]", "987654"],
    ["Joy", "[email protected]", "876543"],
  ];
 
  // (B) ARRAY TO CSV STRING
  var csv = "";
  for (let row of array) {
    for (let col of row) { csv += col + ","; }
    csv += "\r\n";
  }
 
  // (C) CREATE BLOB OBJECT
  var myBlob = new Blob([csv], {type: "text/csv"});
 
  // (D) FILE HANDLER & FILE STREAM
  const fileHandle = await window.showSaveFilePicker({
    suggestedName : "demo.csv",
    types: [{
      description: "CSV file",
      accept: {"text/csv": [".csv"]}
    }]
  });
  const fileStream = await fileHandle.createWritable();
 
  // (E) WRITE FILE
  await fileStream.write(myBlob);
  await fileStream.close();
}
Wild Weasel

Simpan file array

var array = [{
    x: 0,
    y: 0
}];
var a = document.body.appendChild(
    document.createElement("a")
);
a.download = "export.txt";
a.href = "data:text/plain;base64," + btoa(JSON.stringify(array));
a.innerHTML = "download example text";
Outstanding Ocelot

Jawaban yang mirip dengan “Buat file dari array simpan js”

Pertanyaan yang mirip dengan “Buat file dari array simpan js”

Lebih banyak jawaban terkait untuk “Buat file dari array simpan js” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya