MemoryLeak MemoryLeak - 1 year ago 121
React JSX Question

how to get the key of a selected value from a dropdown in reactjs?

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)

Answer Source

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>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download