第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

轉(zhuǎn)換為 CSV 時(shí)如何將數(shù)組值保持在同一列中

轉(zhuǎn)換為 CSV 時(shí)如何將數(shù)組值保持在同一列中

富國滬深 2022-06-09 16:35:52
我正在嘗試將 Object 值轉(zhuǎn)換為 CSV,但 join 方法將我的數(shù)組值分隔到 Excel 中的不同列中。關(guān)于如何避免這種情況的任何想法?功能:window.downloadCsv = function(records) {    console.log(records);    const array = [Object.keys(records)].concat(records);    console.log(array);    let result = array.map(it => {        let objectValues = Object.values(it);        for (let i = 0; i < objectValues.length; i++) {            if (Array.isArray(objectValues[i])) {                //Keep it as array            }        }        return objectValues;    }).join('\n');    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();};可能的記錄輸入:id: "5e8468e2db05ff589ca61b30"title: "Example Application Number 1"status: "Preparing Documents"principalInvestigator: "Mr. Harry Styles"coInvestigators: ["Niall Horan, Liam Payne, Zayn Malik, Louis Tomilson"]partners: nullfunder: "EPSRC Standard research"researchGroup: "MedEng"scheme: "Travel Grant"requestedAmount: nullestimatedAmount: 1234submissionDate: "2020-03-23T00:00:00.000+01:00"startDate: "2020-03-29T00:00:00.000+01:00"estimatedDuration: nullendDate: nullfacility: nullcomments: nulldateCreated: "2020-04-01T12:11:46.783+02:00"lastUpdated: "2020-04-01T12:11:46.783+02:00"dateDeleted: null__proto__: Object結(jié)果的當(dāng)前輸出:id,title,status,principalInvestigator,coInvestigators,partners,funder,researchGroup,scheme5e8468e2db05ff589ca61b30,Example Application Number 1,Preparing Documents,Mr. Harry Styles,Niall Horan, Liam Payne, Zayn Malik, Louis Tomilson期望的輸出:id,title,status,principalInvestigator,coInvestigators,partners,funder,researchGroup,schemeExcel 格式可能更容易理解。目前,導(dǎo)出后是這樣的:https ://imgur.com/2I8h9kl所需的外觀是:https ://imgur.com/bFUKQY2因此,我?guī)缀跸M麑?shù)組值保留在同一列中,而不是將它們分成不同的列,這也會(huì)在CSV.
查看完整描述

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);


查看完整回答
反對 回復(fù) 2022-06-09
?
弒天下

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);


查看完整回答
反對 回復(fù) 2022-06-09
?
慕的地10843

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; }


查看完整回答
反對 回復(fù) 2022-06-09
  • 3 回答
  • 0 關(guān)注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號