0

I am getting an array of object returned from Database.I want to map that array and inside that i want to map another array.But i am getting some error. If anyone knows please help out with this problem.

displayMovies(){
    if(!this.state.body) return ( <h2> Loading.... </h2>)
    else{

        return this.state.body.map((data) => {
            return(
                <div key={data._id}>
                    <li> {data.name} </li>
                    <ul>
                        {
                            return data.map((cast) => {
                                return <li> cast.name </li>
                            })
                        }
                    </ul>
                </div>
            )
        })
    }
}

Data response

3
  • return data.map((cast) => { --> data.map((cast) => { Commented May 29, 2018 at 18:06
  • can you explain it little bit. @YuryTarabanko Commented May 29, 2018 at 18:08
  • You don't need another return. You are in the middle of another return statement. Commented May 29, 2018 at 18:09

2 Answers 2

1

You are not in any function inside nested loop so you do not need to return any thing Replace

 return data.map((cast) => {

to

data.map((cast) => {

and add apply map function on cast property of data

displayMovies(){
    if(!this.state.body) return ( <h2> Loading.... </h2>)
    else{

        return this.state.body.map((data) => {
            return(
                <div key={data._id}>
                    <li> {data.name} </li>
                    <ul>
                        {
                            data.cast.map((cast) => {
                                return <li> {cast.name} </li>
                            })
                        }
                    </ul>
                </div>
            )
        })
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

i tried that and its saying data.map() is not a function
One more thing you are accessing {data.name} name in data so how could you use this as array?
I thing you are missing some property between data and map which should be a array data.'property'.map
I just added the image of response, if that helps
I just updated my answer check now You are missing caste property before map like this data.caste.map
|
0

Your first map function returns an Object. Objects don't have map function use a for in loop instead, inside the loop, check if the property is an object. Replace second map with something like this :

for (const key in object) {
  if (object.hasOwnProperty(key) && typeof(object[key]=='object') {
    // do something with object[key].name 
  }
} 

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.