简体   繁体   中英

How can we get value by selectedOption from react-select npm package?

I am getting array of value label pair in React-Select npm package but I want to get value in string format.Code is given below:-

handleChange = selectedOption => {
    this.setState({ selectedOption });
    console.log(`Option selected:`, selectedOption);
  };

Actual output:- Option selected:[{value:'Apple'   label:'Apple'},{value:'Banana'   label:'Bnanana'}]
Expected Output:- Option selected: Apple,Banana

You can just map through the selected options:

handleChange = selectedOption => {

    const selectedOptionValues = selectedOption.map(({ value }) => value );
    console.log(`Option selected:`, selectedOptionValues);
    this.setState({ selectedOption: selectedOptionValues });

};

You could use map :

 const myArray = [{value:'Apple',label:'Apple'},{value:'Banana',label:'Bnanana'}]; let results = myArray.map(el => el.value); console.log(`Option selected: ${results.toString()}`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM