javascript - Confusion with mapping arrays of objects JS / Next / React
问题描述
Edit: re-wrote question to make it cleaner: I have data like this,
[
{ winrate: '56.67' },
[
{
bout: "Bout 5: Europe's Qualifiers",
wins: '2',
role: 'Alternate'
},
{
pokemon: 'Quagsire',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/195.png'
},
{
pokemon: 'Venusaur',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/3.png'
},
{
pokemon: 'Beedrill',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/15.png'
},
{
pokemon: 'Mawile',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/303.png'
},
{
pokemon: 'Escavalier',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/589.png'
},
{
pokemon: 'Mandibuzz',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/630.png'
}
],
[
{
bout: "Bout 4: Europe's Qualifiers",
wins: '1',
role: 'Alternate'
},
{
pokemon: 'Quagsire',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/195.png'
},
{
pokemon: 'Ninetales_alolan',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/ninetales-alola.png'
},
{
pokemon: 'Beedrill',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/15.png'
},
{
pokemon: 'Sealeo',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/364.png'
},
{
pokemon: 'Jellicent',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/593.png'
},
{
pokemon: 'Venusaur',
sprite: 'https://thesilphroad.com/img/pokemon/icons/96x96/3.png'
}
], ...etc it goes on for a while.
I'm trying to map it in a react component like this:
const Teams = ({ teams }) => {
let pokemap = teams.map((team, index) => (
<React.Fragment key={index}>
<br />
<div class="container">
{team.map((pokemon) => (
<React.Fragment key={pokemon.pokemon}>
<br /> <div class="bout">{pokemon.bout}</div>
<div class="child">
<img src={pokemon.sprite} />
<p>{pokemon.pokemon}</p>
</div>
</React.Fragment>
))}
</div>
<br />
</React.Fragment>
));
return (
<div>
{teams.role}
{teams.winrate}
{pokemap}
</div>
);
};
export default Teams;
I get the error that the nested team.map is not a function, because after the first map, the nested map is just an array containing an object instead of multiple arrays but I have no idea how to get around it.
EDIT: FIXED :) Here's the new component:
const Teams = ({ teams }) => {
console.log(teams);
let pokemap = teams.slice(1).map((team, index) => (
<React.Fragment key={index}>
<br />{" "}
<div class="container">
<div class="bout">{team[0].bout}</div>{" "}
<div>
Score:
<br />
{team[0].wins && team[0].wins + "-"}
{team[0].wins && 3 - team[0].wins}
</div>
<br />
{Array.isArray(team) &&
team.slice(1).map((pokemon) => (
<React.Fragment key={pokemon.pokemon}>
<div class="child">
<img src={pokemon.sprite} />
<p>{pokemon.pokemon}</p>
</div>
</React.Fragment>
))}
</div>
</React.Fragment>
));
return (
<div>
Win Rate: {teams[0].winrate}%{pokemap}
</div>
);
};
解决方案
Since your data contains both objects and arrays you need to check within the first iteration level if the current item is an array and only call map()
in that case:
{Array.isArray(team) && team.map((pokemon) => (
<React.Fragment key={pokemon.pokemon}>
<br /> <div class="bout">{pokemon.bout}</div>
<div class="child">
<img src={pokemon.sprite} />
<p>{pokemon.pokemon}</p>
</div>
</React.Fragment>
))}
推荐阅读
- google-analytics - Google Analytics 事件类别中的“[object Object]”
- java - 为高中生开发一个关于java接口的例子
- amazon-web-services - 带有临时文件系统的 EC2 / LAMP
- java - 从 RESTful API 中提取数据以解析为 json 并从那里提取数据 JAVA
- python - 如何使用 Keras 修复迁移学习的损失
- c# - 派生 XPath 以访问表元素
- python - setup.py 生成带有“#!”的 /usr/bin 包装器。社邦
- gson - 如何使用 gson.fromJson() 基于 json 字符串的数据构建 Gson 以创建不同的 pojo 类
- html - Bootstrap Carousel 渲染但不滑动。左右图标也不起作用
- mysql - 如何查询数据库中的所有表以获取最后一个现有值?