JS Map 混淆

huangapple go评论82阅读模式
英文:

JS Map confusion

问题

尝试从数组获取数据,收到错误。我已将数据传递到组件并访问披萨的数据,该数据是一个数组。请帮忙解决这个映射错误。我在映射中做错了什么?

const mockJson = {
    "pizza": [
        {
            "id": 0,
            "name": "玛格丽特",
            "description": "",
            "ingredients": ["番茄酱", "奶酪"],
            "spicy": false,
            "vegetarian": true,
            "price": 17.0,
            "image": "https://i.imgur.com/8B8YLOo.jpg"
        },
        {
            "id": 1,
            "name": "意大利辣香肠",
            "description": "",
            "ingredients": ["番茄酱", "奶酪", "双层辣香肠"],
            "spicy": false,
            "vegetarian": false,
            "price": 20.0,
            "image": "https://i.imgur.com/OHHctnf.jpg"
        }
    ]
}

const RestaurantCard = (data) => {
    console.log(data, 'data1') //数据可达
    return (
        <>
            <div className="res-card">
                {console.log(helo, 'data2')}
                {data.pizza?.map((helo, id) => {
                    return (
                        <p key={id}>{helo.name}</p>
                        <p key={id}>{helo.name}</p>
                    )
                })}
            </div>
        </>
    )
}

const Body = () => {
    return (
        <>
            <div className="res-container"><RestaurantCard data={mockJson} /></div>
        </>
    )
}

我无法获取 {helo.name}我做错了什么
英文:

Trying to get the data from Array, Received Error. I have passed data into the component
and accessing the data of pizza that is an Array. Please help to solve this mapping
Error. What doing wrong here in mapping ?

const mockJson = {
&quot;pizza&quot;: [
{
&quot;id&quot;: 0,
&quot;name&quot;: &quot;Margherita&quot;,
&quot;description&quot;: &quot;&quot;,
&quot;ingredients&quot;: [&quot;tomato sauce&quot;, &quot;mozzarella&quot;],
&quot;spicy&quot;: false,
&quot;vegetarian&quot;: true,
&quot;price&quot;: 17.0,
&quot;image&quot;: &quot;https://i.imgur.com/8B8YLOo.jpg&quot;
},
{
&quot;id&quot;: 1,
&quot;name&quot;: &quot;Pepperoni&quot;,
&quot;description&quot;: &quot;&quot;,
&quot;ingredients&quot;: [&quot;tomato sauce&quot;, &quot;mozzarella&quot;, &quot;double pepperoni&quot;],
&quot;spicy&quot;: false,
&quot;vegetarian&quot;: false,
&quot;price&quot;: 20.0,
&quot;image&quot;: &quot;https://i.imgur.com/OHHctnf.jpg&quot;
}
]
const RestaurantCard = (data) =&gt; {
console.log(data, &#39;data1&#39;) //data is reachable
return (
&lt;&gt;   
&lt;div className=&quot;res-card&quot;&gt;
{console.log(helo, &#39;data2&#39;)} 
{data.pizza?.map((helo,id) =&gt; 
{
return (
&lt;p key={id}&gt;{helo.name}&lt;/p&gt;
&lt;p key={id}&gt;{helo.name}&lt;/p&gt;
)
}
)}
&lt;/div&gt;
&lt;/&gt;
) 
}
================================= 
const Body = () =&gt; {
return (
&lt;&gt;
&lt;div className=&quot;res-container&quot;&gt;&lt;RestaurantCard data={mockJson} /&gt;
&lt;/div&gt;
&lt;/&gt;
)
}

I'm unable to get {helo.name}, what wrong am i doing ?

答案1

得分: 0

Option one:

// 解构赋值
const RestaurantCard = ({ data }) => {
console.log(data, 'data1') // 可以访问到data
return (
<>
<div className="res-card">
{console.log(helo, 'data2')}
{data?.pizza?.map((helo, id) =>
{
return (
<p key={id}>{helo.name}</p>
<p key={id}>{helo.name}</p>
)
}
)}
</div>
</>
)
}

Option two:

// 作为props访问
const RestaurantCard = (props) => {
console.log(data, 'data1') // 可以访问到data
return (
<>
<div className="res-card">
{console.log(helo, 'data2')}
{props?.data?.pizza?.map((helo, id) =>
{
return (
<p key={id}>{helo.name}</p>
<p key={id}>{helo.name}</p>
)
}
)}
</div>
</>
)
}
英文:

You should either destruct the data prop or receive it using props:

Option one:

// destructuring
const RestaurantCard = ({data}) =&gt; {
console.log(data, &#39;data1&#39;) //data is reachable
return (
&lt;&gt;   
&lt;div className=&quot;res-card&quot;&gt;
{console.log(helo, &#39;data2&#39;)} 
{data?.pizza?.map((helo,id) =&gt; 
{
return (
&lt;p key={id}&gt;{helo.name}&lt;/p&gt;
&lt;p key={id}&gt;{helo.name}&lt;/p&gt;
)
}
)}
&lt;/div&gt;
&lt;/&gt;
) 
}

Option two:

// accessing it as props
const RestaurantCard = (props) =&gt; {
console.log(data, &#39;data1&#39;) //data is reachable
return (
&lt;&gt;   
&lt;div className=&quot;res-card&quot;&gt;
{console.log(helo, &#39;data2&#39;)} 
{props?.data?.pizza?.map((helo,id) =&gt; 
{
return (
&lt;p key={id}&gt;{helo.name}&lt;/p&gt;
&lt;p key={id}&gt;{helo.name}&lt;/p&gt;
)
}
)}
&lt;/div&gt;
&lt;/&gt;
) 
}

huangapple
  • 本文由 发表于 2023年4月17日 04:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定