首页 > 解决方案 > React:将数据从地图函数下载到文件中

问题描述

如何将地图函数中的数据下载到文本文件中?我有以下内容:

const downloadTxtFile = () => {
    const element = document.createElement('a');
    const file = new Blob([document.getElementById('result').innerHTML], { type: 'text/plain' });
    element.href = URL.createObjectURL(file);
    element.download = 'Capture_Download.txt';
    document.body.appendChild(element); // Required for this to work in FireFox
    element.click();
  };

哪个有效并且确实下载了带有数据的文件但我只得到文件中的第一个结果。

{data.map((result, i) => (
            <div className='py-1'>
              <span className='font-mono' id='result'>
                <pre>
                  <span className='text-xs text-gray-400 font-mono'>Capture {i} : </span>
                  <code >{result}</code>
                </pre>
              </span>
              <br />
            </div>
          ))}

result类似于:

捕获 0:0.15

捕获 1:1.68

捕获 2:0.26

捕捉 3:无底价

但我只是进入<pre><span class="text-xs text-gray-400 font-mono">Capture 0 : </span><code>0.15</code></pre>文件。我怎样才能得到文件中的最终结果?

标签: javascriptreactjs

解决方案


推荐阅读