简体   繁体   English

如何从 javascript 中嵌套的 object 下载 CSV?

[英]How to download CSV from a nested object in javascript?

I want to iterate over an object in Javascript and create a CSV file from its internal objects.我想遍历 Javascript 中的 object 并从其内部对象创建一个 CSV 文件。

const list = {
    "right": {
        "label": "Right",
        "items": ["Jack", "Dough"]
    },
    "wrong": {
        "label": "Wrong",
        "items": ["Rain", "Over"]
    },
    "nope": {
        "label": "Nope",
        "items": ["No", "Way"]
    }
};
const downLoadCsv = (list) => {
    let csvContent = '';
    Object.keys(statusM).forEach((obj) => {
      console.log(obj);
      //do something like obj.items and iterate over internal object - items
      //but typeof obj is string here
    });
    const anchorEle = document.createElement('a');
    anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`;
    anchorEle.target = '_blank';
    anchorEle.download = `${key}.csv`;
    anchorEle.click();
  };

I tried to iterate over obj but I found that the typeof obj is string.我试图遍历obj但我发现typeof obj是字符串。 I was rather expecting an object like as follows:-我相当期待 object 如下所示:-

    {
        "label": "Right",
        "items": ["Jack", "Dough"]
    },

I'm expecting an output like this:-我期待这样的 output:-

  Jack, Right
  Dough, Right
  Rain, Wrong
  Over, Wrong
  No, Nope
  Way, Nope

Can someone please help?有人可以帮忙吗?

  • so, you need to conver entire object to the next structure:因此,您需要将整个 object 转换为下一个结构:
[
 ['Jack', 'Right'],
 ['Dough', 'Right'],
]
  • it accomplished via this: Object.values(list).map(i => i.items.map(j => [j, i.label])).flat()它通过这个完成: Object.values(list).map(i => i.items.map(j => [j, i.label])).flat()

  • after that i added extra method toCsv for formatting - to allow break line之后我向toCsv添加了额外的格式化方法 - 允许换行

 const list = { "right": { "label": "Right", "items": ["Jack", "Dough"] }, "wrong": { "label": "Wrong", "items": ["Rain", "Over"] }, "nope": { "label": "Nope", "items": ["No", "Way"] } }; const downLoadCsv = (list) => { let csvContent = toCsv(Object.values(list).map(i => i.items.map(j => [j, i.label])).flat()); const anchorEle = document.createElement('a'); anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`; anchorEle.target = '_blank'; anchorEle.download = `test.csv`; anchorEle.click(); }; function toCsv(arr){ return arr.reduce(function(csvString, row){ csvString += row.join(','); csvString += "\r\n"; //";";//"\n"; return csvString; }, ''); } downLoadCsv(list)

here, you need to put values instead of key but here, I have hide unrelated cuz I am unknown about that code.在这里,您需要输入值而不是键,但在这里,我隐藏了不相关的内容,因为我不知道该代码。

 const list = { "right": { "label": "Right", "items": ["Jack", "Dough"] }, "wrong": { "label": "Wrong", "items": ["Rain", "Over"] }, "nope": { "label": "Nope", "items": ["No", "Way"] } }; const downLoadCsv = (list) => { let csvContent = ''; Object.values(list).forEach((obj) => { console.log(obj); //do something like obj.items and iterate over internal object - items //but typeof obj is string here }); /* const anchorEle = document.createElement('a'); anchorEle.href = `data:text/csv;charset=utf-8,${encodeURI(csvContent)}`; anchorEle.target = '_blank'; anchorEle.download = `${key}.csv`; anchorEle.click(); */ }; downLoadCsv(list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM