You need to enable JavaScript to run this app. Node.js中使用csvtojson转换CSV时如何仅提取指定字段?
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中使用csvtojson转换CSV时如何仅提取指定字段?

实现方法

你拿到的jsonObj就是CSV转换完成的对象数组,只需要修改then回调内的打印逻辑,遍历数组提取对应字段输出即可。

方法1:逐条打印每一项的id和title

csvtojson()
      .fromFile(csvFilePath)
      .then(jsonObj => {
        jsonObj.forEach(item => {
          console.log(`id:${item.id},title:${item.title}`);
        })
      })

方法2:生成仅包含id和title的新数组统一打印

如果你需要后续用到提取后的数据,可以用map方法生成新数组后打印:

csvtojson()
      .fromFile(csvFilePath)
      .then(jsonObj => {
        const filteredData = jsonObj.map(item => ({
          id: item.id,
          title: item.title
        }))
        console.log(filteredData);
      })

内容的提问来源于stack exchange,提问作者Nat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 03:00:03