how to bind a json data which has a key-value pair to select html in reactjs, such that It should display the value in the drop-down and If I choose a value , I should provide the relevant key?

For example:

var optionsdata = [

in the drop-down, it should show "Lion","Giraffe","Zebra",... if I choose, Zebra, I should get the select value (Zebra) as well as the key (103, in the above example)

Hope this answer will help

1) First find selected index which is called key

var selectedIndex =;

2) If you have custom attribute and want to fetch value of that attribute then

var customAtrribute=[selectedIndex].getAttribute('customAtrribute');
4 users liked answer #0dislike answer #04
Nabnit Jha profile pic
Nabnit Jha

You can simply filter out the data from the your object once you get get the value of selected option. I have not used controlled input in my example, If you use that it will be even better.

class App extends React.Component {
  constructor() {
    this.state = {
      optionsdata : [
  handleChange = (e) => {
    var value = this.state.optionsdata.filter(function(item) {
      return item.key ==
  render() {
    return (
      <select onChange={this.handleChange}>
        {, key){  return (
          <option key={key} value={data.key}>{data.value}</option> )

ReactDOM.render(<App/>, document.getElementById('app'));
<script src=""></script>
<script src=""></script>
<div id="app"></div>
3 users liked answer #1dislike answer #13
Shubham Khatri profile pic
Shubham Khatri

Use react-select. It is able to handle id and value elegantly. Map your id to react-select value and your value to react-select label.

You can configure callback which will return you selected value/s. You can even turn on search.

0 users liked answer #2dislike answer #20
luboskrnac profile pic

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).