1

Using this data set to listData state in React:

[
   {
      "Key": "Anchorage TAC 82.tif",
      "LastModified": "2019-04-07T03:25:51.000Z",
      "ETag": "\"9f904f2219d1edf3fa39b171c98de924-6\"",
      "Size": 28135199,
      "StorageClass": "STANDARD"
   },
   {
      "Key": "CSA-L01.pdf",
      "LastModified": "2019-04-07T03:25:36.000Z",
      "ETag": "\"90f166238ae6b1f64120e984be743ba4\"",
      "Size": 3406742,
      "StorageClass": "STANDARD"
   }
]

How can I map the Key values into a drop downlist. I've tried:

<select>
    <option>Please select a file:</option>
        {Object.keys(this.state.listData).map(val =>
        <option value={val.Key}>{val.Key}</option>) }
</select>

and it's not working. I'm getting a blank drop down list.

1 Answer 1

2

listData is an array and Object.keys(this.state.listData) will return array of indexes [0,1,2..]
.You need to remove Object.keys() and apply map() directly on listData

<select>
    <option>Please select a file:</option>
        {
           this.state.listData.map(val =>(
              <option value={val.Key}>{val.Key}</option>
           ) 
        }
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

When I try this I get "this.state.listData.map is not a function"
@Rupp How your state looks like. listData is an array right?

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.