我在数组中嵌套了一组对象。
我理解for循环为什么在行的末尾添加逗号,但我很难找到一种更好的方法来编写它(这对编程来说是全新的(,因为它不会在循环中添加逗号。
有更好的写法吗?
function createCSV() {
// CREATE THE CSV VARIABLE
let csvContent = "data:text/csv;charset=utf-8,"
let headers = "date,account,vendor,amount,recurring,description,notes"
csvContent += headers + "rn"
// ITERATE THE ARRAY INTO THE CSV SEPARATED BY COMMAS
adjustments.forEach(element => {
// EACH ARRAY ITEM
let row
//EACH OBJECT PROPERTY
for (const property in element) {
row += element[property]
row += ','
}
csvContent += row + "rn";
});
// DOWNLOAD THE CSV
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "empower_data.csv");
document.body.appendChild(link); // Required for FF
link.click();
console.log('CSV DOWNLOADED')
}
使用数组和数组加入
function createCSV() {
let csvContent = "data:text/csv;charset=utf-8,";
let headers = "date,account,vendor,amount,recurring,description,notes";
csvContent += headers + "rn";
adjustments.forEach(element => {
// begin changes
let row = [];
for (const property in element) {
row.push(element[property]);
}
csvContent += row.join(',') + "rn";
// end changes
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "empower_data.csv");
document.body.appendChild(link);
link.click();
console.log('CSV DOWNLOADED');
}
更好的是-用取代所有这些
function createCSV() {
let csvContent = "data:text/csv;charset=utf-8,";
let headers = "date,account,vendor,amount,recurring,description,notes";
csvContent += headers + "rn";
adjustments.forEach(element => {
// begin changes
csvContent += Object.values(element).join(',') + "rn";
// end changes
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "empower_data.csv");
document.body.appendChild(link);
link.click();
console.log('CSV DOWNLOADED');
}
或者对于真正整洁的代码
function createCSV() {
let csvContent = "data:text/csv;charset=utf-8,";
let headers = "date,account,vendor,amount,recurring,description,notes";
csvContent += headers + "rn";
// begin changes
csvContent += adjustments.map(
element => Object.values(element).join(',')
).join('rn');
// end changes
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "empower_data.csv");
document.body.appendChild(link);
link.click();
console.log('CSV DOWNLOADED');
}
最后要注意的是:由于对象属性没有定义";命令"-你可能想做一些类似的事情
function createCSV() {
const properties = ['date','account','vendor','amount','recurring','description','notes'];
let csvContent = "data:text/csv;charset=utf-8,";
let headers = "date,account,vendor,amount,recurring,description,notes";
csvContent += headers + "rn";
// begin changes
csvContent += adjustments.map(element => properties.map(prop => element[prop]).join(',')).join('rn');
// end changes
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "empower_data.csv");
document.body.appendChild(link);
link.click();
console.log('CSV DOWNLOADED');
}
在上面的文章中,我假设对象中的属性名称与CSV列标题相同,但当然,情况并非如此(我只是不知道(
最好的方法是使用数组而不是字符串串联,并使用array.join()
:完成此操作
function createCSV() {
// CREATE THE CSV VARIABLE
let csvContent = "data:text/csv;charset=utf-8,"
let headers = "date,account,vendor,amount,recurring,description,notes"
csvContent += headers + "rn"
// ITERATE THE ARRAY INTO THE CSV SEPARATED BY COMMAS
adjustments.forEach(element => {
// EACH ARRAY ITEM
const row = [];
//EACH OBJECT PROPERTY
for (const property in element) {
row.push(element[property]);
}
csvContent += row.join(',') + "rn";
});
// DOWNLOAD THE CSV
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "empower_data.csv");
document.body.appendChild(link); // Required for FF
link.click();
console.log('CSV DOWNLOADED')
}