1

I can not read data from JSON. I checked that the data is stored in the component state from console after the component get mounted. In the return statement, I tried to render the url string from JSON data using map method. When I log the data information from the console, the data can be found in 'this.state.data' or 'elm' but when add 'multimedia[idx]' to the end of it, it says can not find the data. I want to know the reason of it.

this.state.data.map((elm, idx) =>
//...
<div>{elm.multimedia[idx].url}</div>
//...

JSON data shape of multidmedia element logged from the console:

(29) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, undefined, {…}]

0:{
  caption: "string",
  copyright: "string",
  format: "mediumThreeByTwo210",
  height: 140,
  subtype: "photo",
  type: "image",
  url: "https://static01.nyt.com/images/2019/02/05/science/05SCI-ZIMMER1/05SCI-ZIMMER1-mediumThreeByTwo210.jpg",
  width: 210
}

2 Answers 2

1

Short answer

The reason why it cannot read from the the multimedia property is because you are trying to access the multimedia property by using an index generated from the this.state.data property. Therefore, it may be that your current index is greater than the items inside the multimedia array.

Explanation

So, if you have for example...

this.state.data = [
  {multimedia: [{}, {}, {}]},
  {multimedia: [{}, {}, {}]},
  {multimedia: [{}, {}]},
]

Your map will start looking at the first element in the data array, and passing that as the elm variable will be populated with the first element, and so on... but when accessing the 3rd element in the data array, you will get an object with only 2 items in the multimedia array, but since you're accessing the 3rd element of this.state.data, your idx variable will be set to 2, and when trying to access elm.multimedia[idx].url it will fail because elm.multimedia[2] it's undefined for the 3rd element.

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

2 Comments

Your answer is close to the solution. the problems was, there was an undefined element among those 29 element. so when the map methods tried accessed to the element, It ended up in generating error event thought other 28 elements were okay. thank you for your help :)
I'm glad that it helped you :)
1

From what I can see from your data, it looks like your data has an attribute multimedia which contains the array of the objects with the shape that you posted.

So in this case, your code should be something like:

this.state.data.multimedia.map((elm, idx) =>
//...
<div>{elm.url}</div>

Otherwise, if your data is already the array of those objects, you can do directly something like:

this.state.data.map((elm, idx) =>
//...
<div>{elm.url}</div>

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.