首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在nodejs中仅从CSVTOJSON中提取特定字段?

如何在nodejs中仅从CSVTOJSON中提取特定字段?
EN

Stack Overflow用户
提问于 2021-07-05 13:22:46
回答 1查看 42关注 0票数 0

我已经设法使用csvtojson npm包将CSV数据转换为JSON,并成功地在控制台中显示它。

代码语言:javascript
复制
csvtojson()
      .fromFile(csvFilePath)
      .then(jsonObj => {
        console.log(jsonObj);
      })


[
  {
    id: '1',
    title: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
    price: '109.95',
    description: 'Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday',
    category: 'men clothing',
    image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg'
  }
]

现在我想只登录id和title字段,我该怎么做呢?非常感谢提前,并非常感谢任何帮助。再次感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-05 13:58:54

原因是:它是来自console.log值的对象数组。您可以这样做:

代码语言:javascript
复制
console.log(`Id: ${jsonObj[0].id}` and Title: ${jsonObj[0].title})

如果你有多个对象,你可以像这样遍历它:

代码语言:javascript
复制
const jsonObj = [{
    id: '1',
    title: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
    price: '109.95',
    description: 'Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday',
    category: 'men clothing',
    image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg'
  }, {
   id: '2',
    title: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
    price: '109.95',
    description: 'Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday',
    category: 'men clothing',
    image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg'
  }]

jsonObj.forEach(el => console.log(`Id: ${el.id}, title: ${el.title}`));

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68251041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档