Ad
Format An Array With Date Objects And Show Them In A Dropdown
I have an API call which returns an array of objects. Inside these objects, I have a date / timestamp which is formatted as ISO (correct me if I'm wrong).
The code below is right after my render()
method:
const pickerItems = this.props.currentData.trips.map(trip => {
return {
label: trip.start.timestamp,
value: trip.tripId
};
});
console.log(pickerItems);
The console log returns the following:
Array [
Object {
"label": "2019-05-24T10:12:01.123Z",
"value": "1C8EB4E0888640ED9211CB8C563542D0",
},
Object {
"label": "2019-05-24T10:12:01.123Z",
"value": "1C8EB4E0888640ED9211CB8C563542D0",
},
]
My Dropdown component:
<RNPickerSelect items={pickerItems} />
How could I do the below task in most simple & efficient way?
- Make a dropdown list where all the label (which is sent down as items) is the current date formatted as: 2019-05-24 followed by the exact time.
Thanks beforehand, Erik
Ad
Answer
When you are mapping your array, you should change ISO date to normal date and use it like the one below:
const pickerItems = this.props.currentData.trips.map(trip => {
const someDay= new Date(trip.start.timestamp)
return {
label: `${someDay.geyFullYear()}-{someDay.getMonth + 1}-{someDay.getDate() ${someDay.getHours()}:${someDay.getMinutes()}}`,
value: trip.tripId
};
});
console.log(pickerItems);
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad