原因是:它是console.log值中的一个对象数组。你可以这样做:
我已经成功地使用csvtojson npm包将CSV数据转换为JSON,并成功地将其显示在控制台中。
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字段,我应该怎么做呢?非常感谢您的帮助。再次感谢
console.log(`Id: ${jsonObj[0].id}` and Title: ${jsonObj[0].title})
如果你有多个对象,你可以这样循环:
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}`));