首页 > 编程语言 >How to export objects to a csv file using pure JavaScript All In One

How to export objects to a csv file using pure JavaScript All In One

时间:2023-10-17 15:01:19浏览次数:38  
标签:const pure JavaScript How result https xgqfrms CSV com

How to export objects to a CSV file using pure JavaScript All In One

如何使用纯 JavaScript 将对象导出到 CSV 文件

CSV

CSV 使用, / 逗号 分隔

CSV file separator
CSV 文件分隔符

Comma-separated values (CSV)

image

Comma-separated values, a file format and extension

逗号分隔值、文件格式和扩展名

https://en.wikipedia.org/wiki/Comma-separated_values

https://en.wikipedia.org/wiki/CSV

solution

const obj = {
  0: ["1405", "text string 1 #something"],
  1: ["1366",  "text string 2 #hashtag"],
  603: ["92", "text string 603"],
};

const autoDonwload = (obj) => {
  let result = `Views, Title`;
  for(const key in obj) {
    const [num, str] = obj[key];
    result += `\n`;
    result += `${num}, ${str}`;
  }
  console.log(`result =\n`, result);
  const blob = new Blob([result], { type: "text/csv; charset=utf-8;" });
  const url = URL.createObjectURL(blob);
  const link = document.createElement("a");
  link.setAttribute("href", url);
  link.setAttribute("download", "export.csv");
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

demos

<iframe allowfullscreen="true" allowtransparency="true" frameborder="no" height="408.23382568359375" loading="lazy" scrolling="no" src="https://codepen.io/xgqfrms/embed/xxmodaB?default-tab=result&theme-id=dark" style="width: 100%" title="Untitled"> See the Pen Untitled by xgqfrms (@xgqfrms) on CodePen. </iframe>

https://codepen.io/xgqfrms/pen/xxmodaB?editors=1011

![](/i/l/?n=23&i=blog/740516/202310/740516-20231017144546167-1303533680.png)

![image](https://user-images.githubusercontent.com/7291672/275735806-77054f7e-edd6-4865-9bd2-226752d0d5fa.png)

[![enter image description here][1]][1]

  [1]: https://i.stack.imgur.com/sEgtw.png

image

enter image description here

(

标签:const,pure,JavaScript,How,result,https,xgqfrms,CSV,com
From: https://www.cnblogs.com/xgqfrms/p/17769709.html

相关文章

  • JavaScript百炼成仙之前言
    JavaScript是一种轻量级的动态编程语言,在诞生之时,JavaScript并没有受到人们的过多关注,但是谁也想不到,在大前端快速风靡的今天,JavaScript已经成为当今编程世界中优雅而不可或缺的一员。如果你是一名编程初学者,刚刚学习完HTML和CSS,那你就不得不接触JavaScript了。刚开始,你会用jQuery......
  • 【转载】How to solve the problem that getting timestamp from Mysql database is 8
    Thisarticleintroducestherelevantknowledgeof"howtosolvetheproblemofobtainingtimestampfromMysqldatabase8hoursearlierthanthenormaltime".Intheoperationprocessofactualcases,manypeoplewillencountersuchdifficulties.......
  • Javascript报错:Uncaught TypeError: $(...).slide is not a function
    检查网站的时候,发现网页出现一个报错,UncaughtTypeError:$(...).slideisnotafunction同时,平时没有问题的轮播图,也不轮播了。检查并解决步骤如下: 1.顺着错误提示点过去,发现就是slide函数无法运行。查看相关介绍,表示是jq文件进行了重复引用,且版本不同 如下图相关资料描......
  • 2D物理引擎 Box2D for javascript Games 第四章 将力作用到刚体上
    2D物理引擎Box2DforjavascriptGames第四章将力作用到刚体上将力作用到刚体上Box2D是一个在力作用下的世界,它可以将力作用于刚体上,从而给我们一个更加真实的模拟。但是,如果你想要移动刚体,发射子弹,抛掷小鸟,驾驶汽车和当你在玩物理游戏时你看到的一切令人起劲的事情,那么你......
  • How to fix the bug that the beforeunload event cannot be triggered All In One
    HowtofixthebugthatthebeforeunloadeventcannotbetriggeredAllInOne如何修复beforeunload事件无法触发的bugAllInOnebeforeunloadconstbeforeUnloadHandler=(event)=>{event.preventDefault();//Equivalenttothefollowingle......
  • 《Head First JavaScript程序设计》高清高质量 原版电子书PDF+源码
    下载:https://pan.quark.cn/s/9b368a216582......
  • 从零开发一款图片编辑器(使用html5+javascript)
    最近开发了一个图片编辑器,类似于photoshop的网页版,源码参考自GitHub上,顺便也总结下使用html+js开发一个编辑器需要用到哪些知识点。预览地址:https://ps.gitapp.cngithub地址:https://github.com/photopea/photopea架构设计选型:jquery.js和blueimp-canvas.js都是强大的......
  • How can I change the reference numbers in manuscript to blue color?
    HowcanIchangethereferencenumbersinmanuscripttobluecolor?IamworkinginWord2010and EndNoteX7.Iwanttochangethecolorofcitationsinmanuscripttoblue(nottochangethemmanually), buttochangethemautomaticallyincreatingt......
  • r - How do I order by row.names in dataframe R语言 排序
     new_df<-df[order(row.names(df)),]REF:https://stackoverflow.com/questions/20295787/how-can-i-use-the-row-names-attribute-to-order-the-rows-of-my-dataframe-in-rhttps://stackoverflow.com/questions/25194196/how-do-i-order-by-row-names-in-dataframe......
  • 深入理解 JavaScript 时间分片:原理、应用与代码示例解析
    JavaScript时间分片(TimeSlicing)是一种优化技术,用于将长时间运行的任务拆分为多个小任务,以避免阻塞主线程,提高页面的响应性和性能。本文将详细解释JavaScript时间分片的原理、应用场景,并通过代码示例帮助读者更好地理解和应用该技术。本文首发于:kelen.cc概念时间分片(TimeSl......