0

I'm trying to filter an nested array of objects but it's not working as expected....

Here is my json:

[
    {
      "seasson_number": "1",
      "episodes": [
        {
          "number": 1,
          "video_url": "http://test.com",
          "name": "Testing"
        },
        {
          "number": 2,
          "video_url": "http://test.com",
          "name": "Testing"
        }
      ]
    },
    {
      "seasson_number": "2",
      "episodes": [
        {
          "number": 1,
          "video_url": "http://test.com",
          "name": "Testing"
        },
        {
          "number": 2,
          "video_url": "http://test.com",
          "name": "Testing"
        }
      ]      
    }
]

And this is my function to filter by the seasson_number and get the episode name:

const episodios = this.state.seassons
        .filter(seasson => {
            return seasson.seasson_number === "2"; // sample number
        })
        .map(seasson => {
            seasson.episodes.map(episode =>{
                return (
                    <h2>{episode.name}</h2>
                )
            })                
        });
6
  • so your issue is that there's no output? Commented Nov 30, 2017 at 18:54
  • What does your component look like for displaying these episodes? Commented Nov 30, 2017 at 18:55
  • @fodma1 yeah, it returns and single empty <h2></h2> Commented Nov 30, 2017 at 18:56
  • === comparisons check also if the types of variables are the same. Are you sure seasson.seasson_number is a string? Commented Nov 30, 2017 at 18:57
  • @ZachStoltz I'm just trying to display some <h2> inside and <div> Commented Nov 30, 2017 at 18:57

1 Answer 1

3

In your first map function, you should add a return statement:

map(seasson => {
        return seasson.episodes.map(episode =>{
            return (
                <h2>{episode.name}</h2>
            )
        })                
    });
Sign up to request clarification or add additional context in comments.

5 Comments

The first part is not correct because if the seasson_number in the object is "2" than "2" === "2" will return true.
season number is a string, the issue is the first return
I have just thought it's a number because of the variable name "seasson_number". Don't blame me for this :
nobody is blaming you but it is plainly in the original post. The focus of your answer should be the missing return statement and I'd suggest editing it so this answer is clear for any future readers
I approved the modification of Zach Stoltz. Hope this will help other users :)

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.