0

I don't see any error but a blank jsx using es6 map over a nested array of object.

https://codesandbox.io/s/9jpo8z0poy

my data

const data = [
  {
    "name": "Main Food",
    "categories": [
      {
        "name": "Sub Food",
        "nature_of_business": [
          {
            "name": "Wholesaler"
          },
          {
            "name": "Direct sales"
          }
        ]
      }
    ]
  }
]

Jsx

<div>
  natural of business: {
    (data.categories || [])
      .map(o2 => o2.nature_of_business || [])
      .map(o3 => o3.name)
      .join(', ')
  }
</div>

1 Answer 1

3

You're mapping the data incorrectly. You have to get the first item of the array, not data.categories:

Edit: Re-written for easier ES6 reading.

 <div>
    natural of business: {
       data[0].categories.map(category => category.nature_of_business.map(cat => 
           <div>{cat.name}</div>
       ))
    }
 </div>

Snippet of the mapping:

const data = [
  {
    "name": "Main Food",
    "categories": [
      {
        "name": "Sub Food",
        "nature_of_business": [
          {
            "name": "Wholesaler"
          },
          {
            "name": "Direct sales"
          }
        ]
      }
    ]
  }
];

data[0].categories.map(category => category.nature_of_business.map(cat => console.log(cat.name)));

Sign up to request clarification or add additional context in comments.

3 Comments

I want print the value in jsx
@SharonChai just add a <div> container for the cat.name that is returned from the last map and it should print your values in jsx :) like this => <div>{cat.name}</div>
ouch I didn't realised I need to have map within map.

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.