使用 ES6 在 React 中映射对象内部的数组
Mapping an array inside an object in React with ES6
我正在制作一个 react-feed 应用程序,灵感来自 Michael's tutorial (there's also a video) and I encountered some trouble trying to pass an array inside an array as props using Lodash's _.map 功能。这是我映射的信息:
const events = [
{
event: 'Gods',
venue: 'Asgard',
venuePicture: 'picture1.jpg',
when: '21:00 27/04/16',
genres: ['rock', 'funk'],
artists: [
{
artist: 'Thor',
artistPicture: 'thor.jpg'
},
{
artist: 'Loki',
artistPicture: 'loki.jpg'
}
]
},
{
event: 'Humans',
venue: 'Midgard',
venuePicture: 'picture2.jpg',
when: '21:00 27/04/16',
genres: ['jazz', 'pop'],
artists: [
{
artist: 'Human1',
artistPicture: 'human1.jpg'
},
{
artist: 'Human2',
artistPicture: 'human2.jpg'
}
]
}
];
我正在像这样传递给组件(这有效):
renderItems(){
const props = _.omit(this.props, 'events');
return _.map(this.props.events, (event, index) => <EventsFeedItem key={index} {...event} {...props}/>);
}
render() {
return (
<section>
{this.renderItems()}
</section>
);
}
这工作得很好,划分每个 "event" 对象
然后我尝试像这样解构和映射每个事件的 "artists:" 对象:
renderArtists() {
const { event, venue, venuePicture, when, genres, artists } = this.props.events;
const props = _.omit(this.props, 'events');
return _.map({artists}, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);
}
render() {
return (
<ul>
{this.renderArtists()}
</ul>
);
}
这是我得到的结果,很接近,但不是我需要的:
我需要进一步分离它们以获得:
{artist: "Thor"} {artistPicture: "thor.jpg"}
{artist: "Loki"} {artistPicture: "loki.jpg"}
等等...
我看到这里有一个模式,我只是不知道如何进一步实现它。当我尝试重复相同的解构然后 _.map 事情时,它会中断。任何人都可以帮我解决这个问题,很抱歉 post.
哦,感谢VyvIT的评论(他删除了他的评论),我找到了问题,它在这里:
const { event, venue, venuePicture, when, genres, artists } = this.props.events;
_.map({artists}, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);
"artists"不应该解构(大括号),应该是这样的:
_.map(artists, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);
非常感谢你们!
return _(this.props.events).flatMap('artists').map((artist, index)=><ItemArtist key={index} {...artist} {...props}/>).value();
我正在制作一个 react-feed 应用程序,灵感来自 Michael's tutorial (there's also a video) and I encountered some trouble trying to pass an array inside an array as props using Lodash's _.map 功能。这是我映射的信息:
const events = [
{
event: 'Gods',
venue: 'Asgard',
venuePicture: 'picture1.jpg',
when: '21:00 27/04/16',
genres: ['rock', 'funk'],
artists: [
{
artist: 'Thor',
artistPicture: 'thor.jpg'
},
{
artist: 'Loki',
artistPicture: 'loki.jpg'
}
]
},
{
event: 'Humans',
venue: 'Midgard',
venuePicture: 'picture2.jpg',
when: '21:00 27/04/16',
genres: ['jazz', 'pop'],
artists: [
{
artist: 'Human1',
artistPicture: 'human1.jpg'
},
{
artist: 'Human2',
artistPicture: 'human2.jpg'
}
]
}
];
我正在像这样传递给组件(这有效):
renderItems(){
const props = _.omit(this.props, 'events');
return _.map(this.props.events, (event, index) => <EventsFeedItem key={index} {...event} {...props}/>);
}
render() {
return (
<section>
{this.renderItems()}
</section>
);
}
这工作得很好,划分每个 "event" 对象
然后我尝试像这样解构和映射每个事件的 "artists:" 对象:
renderArtists() {
const { event, venue, venuePicture, when, genres, artists } = this.props.events;
const props = _.omit(this.props, 'events');
return _.map({artists}, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);
}
render() {
return (
<ul>
{this.renderArtists()}
</ul>
);
}
这是我得到的结果,很接近,但不是我需要的:
我需要进一步分离它们以获得:
{artist: "Thor"} {artistPicture: "thor.jpg"}
{artist: "Loki"} {artistPicture: "loki.jpg"}
等等...
我看到这里有一个模式,我只是不知道如何进一步实现它。当我尝试重复相同的解构然后 _.map 事情时,它会中断。任何人都可以帮我解决这个问题,很抱歉 post.
哦,感谢VyvIT的评论(他删除了他的评论),我找到了问题,它在这里:
const { event, venue, venuePicture, when, genres, artists } = this.props.events;
_.map({artists}, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);
"artists"不应该解构(大括号),应该是这样的:
_.map(artists, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);
非常感谢你们!
return _(this.props.events).flatMap('artists').map((artist, index)=><ItemArtist key={index} {...artist} {...props}/>).value();