Stack Overflow Asked by zosozo on December 27, 2020
I would like to return an array with two values based on the selected option in a dropdown menu so that I can access it using selectedDate[0] and selectedDate[1] but right now I am getting an array with one long string like ["date1, date2"]. How do I get [date1, date2] instead?
At the top I have:
const [ selectedDate, setSelectedDate ] = useState('');
const startDateArray = ['Oct. 18, 2020', 'Oct. 25, 2020'];
const endDateArray = ['Oct. 24, 2020', 'Oct. 31, 2020'];
and this is my code:
<form>
<div className="form-group">
<select className="form-control" id="date-range" disabled={loading} value={selectedDate}
onChange={(e) => setSelectedDate([e.currentTarget.value)]}>
{startDateArray.reverse().map((value, index) => (
<option key={index} value={[moment(value), moment(endDateArray[index])]}>
{value}-{endDateArray[index]}
</option>
))}
</select>
</div>
</form>
This code will split the string and make an array out of the individual elements.
It answers this: "How do I get [date1, date2] instead?"
const input = ["date1, date2"];
const selectedDate = input[0].split(',').map(s=>s.trim());
console.log(selectedDate[0], selectedDate[1]);
Correct answer by Randy Casburn on December 27, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP