I have a nested map to iterate through my data and then return a react component for each object I have in my Data. Check the code below:
return props.dataArray.map((classObj) => {
return classObj.timetable.map((timetableObj) => {
if (timetableObj.weekday === props.day) {
return ttCard({
lecturer: classObj.lecturer,
module: classObj.module,
room: classObj.room,
class_id: classObj.id,
timetable_id: timetableObj.id,
startTime: timetableObj.start_time,
endTime: timetableObj.end_time,
weekday: timetableObj.weekday
});
}
return null;
});
});
My issue is that my component aren't sorted and they have to be sorted because it is a timetable for students. Look at the image below and notice how 10:30 AM is the latest card in my tab view:
things I tried so far:
1- creating a new array then sorting it after the if statement
block - not working 2- returning a sorted array to a function and mapping that array to return the component - not working 3- looping and returning the object as a function within the component parameter:
ttCard( sortedArr.forEach((ttObject) => {
return ttObject
}))
also not working
I have googled and tried to research but no hope so far. I know that initially my nested map is bad but this is how my object looks like:
[
{
"class_code": "English-5-G-2-L-1911-test-teacher",
"id": 23,
"lecturer": "test teacher",
"module": "English-5",
"room": "L",
"timetable": [
{
"end_time": "9:00",
"id": 37,
"start_time": "8:00",
"weekday": "Sunday"
},
{
"end_time": "10:00",
"id": 38,
"start_time": "9:00",
"weekday": "Sunday"
},
{
"end_time": "10:00",
"id": 47,
"start_time": "9:00",
"weekday": "Monday"
},
{
"end_time": "10:00",
"id": 48,
"start_time": "9:00",
"weekday": "Tuesday"
},
{
"end_time": "11:30",
"id": 52,
"start_time": "10:30",
"weekday": "Tuesday"
},
{
"end_time": "11:30",
"id": 54,
"start_time": "10:30",
"weekday": "Thursday"
},
{
"end_time": "12:30",
"id": 58,
"start_time": "11:30",
"weekday": "Thursday"
},
{
"end_time": "14:00",
"id": 61,
"start_time": "13:00",
"weekday": "Wednesday"
}
]
}
]
You can use sort
on your output to arrange the array by startTime
.sort((a, b) => {
// These lines are so we can directly compare the times
// I would recommend to send timestamps down with your data (if possible)
var aTime = a.startTime.replace(':', '');
var bTime = b.startTime.replace(':', '');
// Sort by smallest first
return aTime - bTime;
});
I would also suggest filtering out null values in your array
Using your example -
var classesArr = props.dataArray.map((classObj) => {
return classObj.timetable.map((timetableObj) => {
if (timetableObj.weekday === props.day) {
return {
lecturer: classObj.lecturer,
module: classObj.module,
room: classObj.room,
class_id: classObj.id,
timetable_id: timetableObj.id,
startTime: timetableObj.start_time,
endTime: timetableObj.end_time,
weekday: timetableObj.weekday
}
}
return null;
}).filter(x => x); // Filter out any null values
}).filter(x => x); // Filter out any null values.
// Flatten array so it is sortable/mapable
return [].concat.apply([], classesArr).sort((a, b) => {
// These lines are so we can directly compare the times
// I would recommend to send timestamps down with your data (if possible)
var aTime = a.startTime.replace(':', '');
var bTime = b.startTime.replace(':', '');
// Sort by smallest time first
return aTime - bTime;
}).map((x) => {
return ttCard(x);
})
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.