javascript - 使用 JavaScript 将对象数组转换为 TSV 格式(无在线工具)
问题描述
大批:
[{"username":"admin","datetime":"27/10/2018 11:27","password":"12345"}]
至
TSV 格式:
username datetime password
admin 27/10/2018 11:27 12345
我用谷歌搜索过,但大多数情况下我找到了上面的在线工具。任何人都可以为此建议脚本,在此先感谢。
免责声明:我的应用程序在没有互联网的情况下工作,因此互联网依赖在这里不起作用。
解决方案
const data = [{"username":"admin","datetime":"27/10/2018 11:27","password":"12345"},{"username":"admin2","datetime":"22/11/2018 11:27","password":"admin2"}];
// grab the column headings (separated by tabs)
const headings = Object.keys(data[0]).join('\t');
// iterate over the data
const rows = data.reduce((acc, c) => {
// for each row object get its values and add tabs between them
// then add them as a new array to the outgoing array
return acc.concat([Object.values(c).join('\t')]);
// finally joining each row with a line break
}, []).join('\n');
// display the result (here with `innerText` because
// `textContent` doesn't recognise styling)
document.body.innerText = `${headings}\n${rows}`;
body { white-space: pre; ]
推荐阅读
- php - 在 WooCommerce 结帐中为特定产品类别制作所需的订单注释
- java - 从 int 数组创建对象数组
- ios - Xcode Objective-C 应用程序如何读取由 Visual Studio Xamarin.IOS 应用程序写入 MyDocuments 目录的文件?
- python - 删除没有元数据(标题、艺术家等)的目录中的特定 mp3 文件?
- python - 在每个步骤中迭代命令时更改日期时间变量?
- javascript - 为什么我的 for 循环不访问该对象内的数组?
- javascript - 如何在javascript中转义@或如何从包含@的字段中检索数据
- r - Bioconductor 封装开发 S3 与 S4
- php - 带有 php 的 Google Sheets API - 查找单元格值
- vue.js - Vue.js 计数器变量导致无限循环