我正在嘗試使用 .map 解構我的物件陣列?我嘗試了幾件事,但一直卡住。我可以通過使用索引來訪問單個資料,但是卻在為如何為它們做這件事而苦苦掙扎。
我知道是這樣的
const {name} = product;
and
const {attributes:name} = product;
but they don't work, they print "undefined".
也試過這個 -
console.log(product.map(({name}) => (console.log(name))))
This is the data
console.log(product);
{
"attributes": {
"image": {
"data": []
},
"name": "test",
"description": "test product",
"price": 20,
"quantity": 0
}
}
這就是我想要實作的
<div className='grid grid-cols-4'>
{product.map(({attributes: name})=> (
<p>{name}</p>
))}
</div>
uj5u.com熱心網友回復:
可以破壞嵌套物件。
嘗試這樣的事情:
product.map(({attributes:{name}})=> ...
uj5u.com熱心網友回復:
- 因為它是物件,所以這將起作用
const { name } = product.attributes;
要么
<p>{product.attributes.name}</p>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/444605.html
標籤:javascript 数组 反应 下一个.js
上一篇:如何將文本添加到特定的映射元素?
