title: How to convert Object to csv file with a multiple sheet in excel/csv file by separating bullet of Object
body: <p>I need some recommandation to convert below object to csv file by <strong>seperating each bullet to new sheet</strong> in csv/excel file".</p>
  
  <pre><code>var data = {
    DeviceA: {
      smokeSensor: [
        {
          '190501': {
            '0001': 200,
            '0002': 300
          },
        },
        {
          '190502': {
            '0001': 20,
            '0002': 30
          },
        }
      ],
      fireSensor: [
        {
          '190501': {
            '0001': 700,
            '0002': 750
          },
        },
        {
          '190502': {
            '0001': 780,
            '0002': 630
          },
        }
      ]
    },
    DeviceB: {
      smokeSensor: [
        {
          '190601': {
            '0001': 100,
            '0002': 110
          },
        },
        {
          '190602': {
            '0001': 120,
            '0002': 130
          },
        }
      ],
      fireSensor: [
        {
          '190601': {
            '0001': 600,
            '0002': 522
          },
        }
      ]
    },
  };
  </code></pre>
  
  <p>Currently, I've done to convert data with my way and got result of excel file as well.</p>
  
  <pre><code>var data = { deviceA: { smokeSensor: [ { '190501': { '0001': 200, '0002': 300 }, }, { '190502': { '0001': 20, '0002': 30 }, } ], fireSensor: [ { '190501': { '0001': 700, '0002': 750 }, }, { '190502': { '0001': 780, '0002': 630 }, } ] }, deviceB: { smokeSensor: [ { '190601': { '0001': 100, '0002': 110 }, }, { '190602': { '0001': 120, '0002': 130 }, } ], fireSensor: [ { '190601': { '0001': 600, '0002': 522 }, } ] }, };
  
  const dataToCSV = data =&gt; {
    const rows = [];
  
    for (const device in data) {
      rows.push(
        [device.replace(/^./, m =&gt; m.toUpperCase())], 
        ["Date/Time", ...Object.keys(data[device])]
      );
      const groups = {};
      let longest = 0;
  
      for (const sensor in data[device]) {  
        for (const time of data[device][sensor]) {
          const k = Object.keys(time)[0];
  
          for (const hm in time[k]) {
            const groupKey = `${k} ${hm.replace(/(\d\d)(\d\d)/, "$1:$2")}`;
  
            if (!(groupKey in groups)) {
              groups[groupKey] = [groupKey];
            }
  
            groups[groupKey].push("" + time[k][hm]);
            longest = Math.max(longest, groups[groupKey].length);
          }
        }
      }
  
      for (const group of Object.values(groups)) {
        while (group.length &lt; longest) { 
          group.push("");
        }
  
        rows.push(group);
      }
  
      rows.push([""]);
    }
  
    return rows.slice(0, -1);
  };
  
  const rows = dataToCSV(data)
  
  /* const rows = [
    ["DeviceA"]
    ["Date/Time", "smokeSensor", "fireSensor"],
    ["190501 00:01", "200", "700"],
    ["190501 00:02", "300", "750"],
    ["190502 00:01", "20", "780"],
    ["190502 00:02", "30", "630"],
    [""],
    ["DeviceB"],
    ["Date/Time", "smokeSensor", "fireSensor"],
    ["190501 00:01", "100", "600"],
    ["190501 00:02", "110", "522"],
    ["190502 00:01", "120", ""],
    ["190502 00:02", "130", ""],
  ];
  */
  
  function exportToCSV(rows) {
        let csvContent = ""
        rows.forEach(function(rowArray) {
            let row = rowArray.join(",")
            csvContent += row + "\r\n"
        });
        var link = document.createElement("a")
        document.body.appendChild(link)
        // var blob = new Blob([csvContent], {type: "text/csv"}); 
        var blob = new Blob([csvContent], {type: "xls/xlsx"})
        var url = window.URL.createObjectURL(blob)
        link.setAttribute("href", url)
        link.setAttribute("download", "my_data.csv")
        link.click() /* Download the data file named "my_data.csv". */
        $('#loading-modal').modal('hide');
  },
  
  exportToCSV(rows)
  
  </code></pre>
  
  <p>All above seems OK. But it all shown in one sheet file if I have much data over 1,000 record then I have a problem to scroll down to see DeviceB, Right?. So, I want to separate DeviceB to new one sheet in excel file. What do I have to do more from exportToCSV function?</p>