3 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試將數(shù)組轉(zhuǎn)換為字符串,然后用另一個(gè)分隔符替換逗號,例如分號 - 這樣 csv 解釋器不會(huì)拆分?jǐn)?shù)組中的值并將其計(jì)為單個(gè)項(xiàng)目
let records = {id: "5e8468e2db05ff589ca61b30",
title: "Example Application Number 1",
status: "Preparing Documents",
principalInvestigator: "Mr. Harry Styles",
coInvestigators: ["Niall Horan, Liam Payne, Zayn Malik, Louis Tomilson"]}
const array = [Object.keys(records)].concat(records);
let result = array.map(it => {
let objectValues = Object.values(it);
for (let i = 0; i < objectValues.length; i++) {
if (Array.isArray(objectValues[i])) {
objectValues[i]= objectValues[i]=(`[${objectValues[i].join(';')}]`).replace(/,/g, ';')
}
}
return objectValues;
}).join('\n');
console.log(result);

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
我個(gè)人建議稍微改變你的解決方案。您不需要連接鍵和值,這會(huì)使.map()不必要的復(fù)雜化。這是我要做的:
var keys = Object.keys(records);
// we take the array of values, but we transform any array
// in a string of values separated by +
var values = Object.values(records).map(record => {
if (Array.isArray(record))
return record.join("+");
return record;
});
// Now we can just put everything together
var result = keys.join(",") + "\n" + values.join(",");
console.log(result);

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試始終設(shè)置一個(gè)最小的可重現(xiàn)示例(https://stackoverflow.com/help/minimal-reproducible-example)
這是我的:
最重要的部分是:
const headers = Object.keys(records).join(',')
const values = Object.values(records).map(child => {
if (child instanceof Array){
//return child.join('; ');
const str = JSON.stringify(child).replace(/"/g, "'");
return `"${str}"`;
}else{
return child;
}
}).join(',')
const result = [headers, values].join('\n')
我們將鍵和值分別放入一個(gè)數(shù)組中,然后將它們放入一個(gè)數(shù)組中并用新行連接它們[headers, values].join('\n')
在地圖內(nèi),您可以執(zhí)行以下任一操作:
const values = Object.values(records).map(child => {
if (child instanceof Array){
const str = JSON.stringify(child).replace(/"/g, "'");
return `"${str}"`;
}else{
return child;
}
}).join(',')
這使得數(shù)組字符串在 Excel 中顯示如下:
"['Niall Horan','Liam Payne','Zayn Malik','Louis Tomilson']"
或者你可以像這樣做地圖:
const values = Object.values(records).map(child => {
if (child instanceof Array){
return child.join('; ');
}else{
return child;
}
}).join(',')
然后 Excel 中的輸出是這樣的(除非您使用該語言環(huán)境,否則分號不會(huì)被讀取為列分隔符 - 即德語語言環(huán)境):
"Niall Horan; Liam Payne; Zayn Malik; Louis Tomilson"
const recordObj = {
id: "5e8468e2db05ff589ca61b30",
title: "Example Application Number 1",
status: "Preparing Documents",
principalInvestigator: "Mr. Harry Styles",
coInvestigators: ["Niall Horan", "Liam Payne", "Zayn Malik", "Louis Tomilson"],
partners: null,
funder: "EPSRC Standard research",
researchGroup: "MedEng",
scheme: "Travel Grant",
requestedAmount: null,
estimatedAmount: 1234,
submissionDate: "2020-03-23T00:00:00.000+01:00",
startDate: "2020-03-29T00:00:00.000+01:00",
estimatedDuration: null,
endDate: null,
facility: null,
comments: null,
dateCreated: "2020-04-01T12:11:46.783+02:00",
lastUpdated: "2020-04-01T12:11:46.783+02:00",
dateDeleted: null
}
downloadCsv(recordObj)
function downloadCsv(records) {
//console.log(records);
const headers = Object.keys(records).join(',')
const values = Object.values(records).map(child => {
if (child instanceof Array){
//return child.join('; ');
const str = JSON.stringify(child).replace(/"/g, "'");
return `"${str}"`;
}else{
return child;
}
}).join(',')
const result = [headers, values].join('\n')
//console.log(headers);
//console.log(values);
console.log(result);
let hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(result);
hiddenElement.target = '_blank';
hiddenElement.download = 'records.csv';
hiddenElement.click();
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
添加回答
舉報(bào)