2

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 (here's a screenshot of it working)

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:enter image description here

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.

2
  • 1
    There's no such thing as a "JSON Array" Commented Apr 24, 2016 at 17:41
  • 1
    what about using _.get? I think it will serve purpose, also could you uplod this to jsbin or some good place where we could test this? Commented Apr 24, 2016 at 18:05

2 Answers 2

1
return _(this.props.events).flatMap('artists').map((artist, index)=><ItemArtist key={index} {...artist} {...props}/>).value();
Sign up to request clarification or add additional context in comments.

2 Comments

Your first comment (the one you deleted) solved my problem VyvIT, thanks a lot! I can't believed I spent all this time and I didn't notice it :D
No problem, here's a solution to flatMap the artists from the initial events array.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.