I have used react-native-material-dropdown in my react native project. I am getting a data from API.

The issue is i am getting name successfully in drop-down but i want a hotelId of that selected name. I am not able to parse the whole JSON in the drop-down.

JSON :

    {
            "message": "Success",
            "notificationCount": 0,
            "result": [{
                "hotelId": "india3c71a2b8-05d3-476e-825a-f31e08ac3bdc",
                "name": "Hotel Samrat"
            }, {
                "hotelId": "indiae4ef0d05-f46e-48f3-9bce-a37bfde089a4",
                "name": "Hotel Kalinga Ashok"
            }],
            "status": 1,
            "totalRecord": 2,
            "scopeId": ""
   }

I have parsed data by :

var count = Object.keys(responseJSON.result).length;
            let drop_down_data = [];
            for (var i = 0; i < count; i++) {
                console.log(responseJSON.result[i].name) // I need to add 
                drop_down_data.push({ value: responseJSON.result[i].name }); // Create your array of data
            }

And added that array in dropdown :

  <Dropdown
       label={'Select hotel name'}
       data={this.state.data}
       baseColor='#79292F'
  />

I have gone through all stack overflow answers. but i ma not able to get the exact answer.

2 Answers

3
sathish kumar On Best Solutions

You can pass data as {value : "", label : ""}

var count = Object.keys(responseJSON.result).length;
            let drop_down_data = [];
            for (var i = 0; i < count; i++) {
                console.log(responseJSON.result[i].name) // I need to add 
                drop_down_data.push({
                value : responseJSON.result[i]. hotelId
                label: responseJSON.result[i].name 
               }); // Create your array of data
            }

//pass your drop_down data to dropdown component 
  <Dropdown
       label={'Select hotel name'}
       data={YOURFINALDATAOBJECT}
       baseColor='#79292F'
  />
3
ma_dev_15 On
const drop_down_data = responseJSON.result.map(element => ({
  value: element.name,
  label: element.hotelId
}));

valueExtractor = val => {
  console.log(val);
};

<Dropdown
  label={"Select hotel name"}
  data={this.state.data}
  baseColor="#79292F"
  valueExtractor={this.valueExtractor}
/>;