【发布时间】:2017-11-10 13:42:55
【问题描述】:
我有一个这样的对象数组:
var matchs = [
{
id: 10689,
sport: 'Tennis',
players: [
{
id: 22,
name:'Rafa Nadal',
country: 'Spain',
odds: {
bookie_1: 1.60,
bookie_2: 1.61,
bookie_3: 1.62
}
},
{
id: 23,
name:'Roger Federer',
country: 'Spain',
odds: {
bookie_1: 2.30,
bookie_2: 2.31,
bookie_3: 2.32
}
}
]
},
{
id: 12389,
sport: 'Tennis',
players: [
{
id: 45,
name:'Fernando Verdasco',
country: 'Spain',
odds: {
bookie_1: 3.20,
bookie_2: 3.21,
bookie_3: 3.22
}
},
{
id: 65,
name:'Andy Murray',
country: 'Spain',
odds: {
bookie_1: 1.20,
bookie_2: 1.21,
bookie_3: 1.22
}
}
]
}
];
我有一个 React 组件,它显示了 map 内的所有匹配 map 。我试图做这样的事情没有好的结果。
let country = 'spain';
const List = ({ matchs }) => {
return (
<div>
{matchs.map((match) =>
let my_filter = match.players.filter(player => player.country === country);
{my_filter ? (
<div className="match" key={match.id}>
<div className="id">{match.id}</div>
<div className="players">
{match.players.map((player, num) =>
<div className="player" key={num}>
<span className="player-country">{player.country}></span>
<span className="name">{player.name}</span>
</div>
)}
</div>
) : ()}
)}
</div>
);
}
这样做的正确方法是什么?
【问题讨论】:
-
你能解释一下你到底想做什么吗?
-
在您的对象中使用
spain进行过滤应该得到什么结果??
标签: javascript reactjs multidimensional-array ecmascript-6