简体   繁体   中英

Mapping an array inside an object in React with ES6

I'm making a react-feed app, inspired by 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 function. This is the information I'm mapping:

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'
                        }
                    ]

                }


             ];

I'm passing to the component like this (this works):

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>
            );
        }

This works perfectly fine, dividing each "event" object(这是它工作的屏幕截图)

Then I try to destructure and map the "artists:" object of each event like this:

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>
        );
    }

This is the result I'm getting, which is close, but not what I need: 在此处输入图片说明

I need to separate these further to get:

{artist: "Thor"} {artistPicture: "thor.jpg"}
{artist: "Loki"} {artistPicture: "loki.jpg"}

and so on...

I see there's a pattern here, I just don't know how to implement it further. It breaks when I try to repeat the same destructure then _.map thing. Can anyone please give me a hand with this, sorry for the long post.

return _(this.props.events).flatMap('artists').map((artist, index)=><ItemArtist key={index} {...artist} {...props}/>).value();

Oh, I found the problem thanks to VyvIT 's comment (he deleted his comment), it's here:

const { event, venue, venuePicture, when, genres, artists } = this.props.events;
    _.map({artists}, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);

"artists" shouldn't be destructured (curly brackets), should be like this:

_.map(artists, (artist, index) => <ItemArtist key={index} {...artist} {...props}/>);

Thank you so much guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM