首页 > 解决方案 > React map 函数没有结果

问题描述

我有一个简单的数组:

const plaene = [
{
"bezeichnung":"P1",
"erstelltAm":"18.07.2020"
},
{
"bezeichnung":"P2",
"erstelltAm":"18.07.2020"
 },
{
"bezeichnung":"P3",
"erstelltAm":"18.07.2020"
 }
]

因此,如果我想映射该数组,我没有得到任何结果。只有当我在地图函数(plan 1 .bezeichnung)中明确指定位置时,我才能得到显示的值。那么为什么以下不起作用?

export default function Wochen({pl}) {

return ( <div>
    {
        Object.entries(pl).map(plan =>{
            return(
                <Plaene key={plan} title={plan.bezeichnung}/> 

            );
        })
    }
    </div> )
};

在此处输入图像描述

标签: javascriptreactjs

解决方案


我会尝试不Object.entries()作为:

{
     pl.map((plan, index) => {
         return(
             <Plaene key={index} title={plan.bezeichnung}/> 
         );
     })
}

上面有你的数组每次迭代.map()的实际条目,也可以用作你的组件的键。planindex


推荐阅读