如何从这个映射数据中使用存储在数组 cottageGallery
中的数据?
const galleryData = components.map(g => {
return {
cottageGallery: g.cottageGallery,
destinationGallery: g.destinationGallery,
activitiesGallery: g.activitiesGallery,
}
})
我以为它只是 const cottageGallery = galleryData.cottageGallery
但它返回未定义。
最佳答案
不完全是,galleryData
将成为一个数组而不是一个对象,因为您正在使用 javascript 的 map
方法。如果您想获取数组的第一项,您可以执行以下操作 - [0]
是数组的第一项。
const cottageGallery = galleryData[0].cottageGallery;
要记录每个 cottageGallery
,您可以使用 forEach
并执行以下操作:
galleryData.forEach(item => {
console.log(item.cottageGallery);
})
关于javascript - 定义数组中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51493942/