在ReactJS中过滤嵌套对象

时间:2017-11-30 18:52:24

标签: javascript reactjs

我试图过滤嵌套的对象数组,但它没有按预期工作....

这是我的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"
        }
      ]      
    }
]




这是我通过seasson_number过滤并获取剧集名称的功能:

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

1 个答案:

答案 0 :(得分:3)

在你的第一个map函数中,你应该添加一个return语句:

map(seasson => {
        return seasson.episodes.map(episode =>{
            return (
                <h2>{episode.name}</h2>
            )
        })                
    });