npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

js2csv

v1.1.0

Published

Convert or render JSON to CSV

Downloads

63

Readme

js2csv

一个可链式调用的将 jsonjs 对象转换或导出为 CSV 的包。

安装


# install
npm install js2csv --save

使用示例


import js2csv from 'js2csv'

const data = [
  {
    name: 'wang',
    email: '[email protected]',
    phone: '12312312312'
  },
  {
     name: 'zhang',
     email: '[email protected]',
     phone: '12312312312'
   },
   {
     name: 'li',
     email: '[email protected]',
     phone: '12312312312'
   }
]

const heads = {
  name: '姓名',
  email: '邮箱',
  phone: '手机号'
}

// 解析并下载

js2csv.setData(data).setHead(heads).setName('花名册').parse().download()

// 解析并返回字符串

let string = js2csv.setData(data).setHead(heads).setName('花名册').render()

console.log(string) // 返回解析后的csv字符串

// transform 接受一个函数,该函数接受一个行为参数

js2csv.setData(data).setHead(heads).setName('花名册').transform(function (row) {
  // 返回新的行数据
  return {
    name: row.name,
    email: row.email,
    phone: row.phone
  }
}).parse().download()

可用方法

|方法名称|参数类型|默认值|说明|返回值| |----|----|----|----|----| |setData()|Array|[]|*必须, 待转换的原始数据|this| |setHead()|Object|{}|设置表头,如果为空或忽略导出数据不包含表头|this| |setName()|String|Date.now() + '.csv'|导出文件名,如果为空或忽略将以当前时间戳为文件名|this| |transform|Function|无|接收一个函数。该函数以当前处理行作为参数并返回经过处理的新行|this| |parse()|无|无|格式化数据|this| |download()|无|无|下载文件|无| |render()|无|无|返回CSV格式字符串|格式化后字符串|

IE浏览器支持

download() 方法支持 IE >= 10

如果需要低版本支持,可使用 render() 方法获取格式化字符串,自行编写下载文件方法。

MIT © JiaxinCui