I have a complex query I'm trying to make. I've got courses that have 9 weeks. I'm trying to trigger an email based on the dates of those nine weeks. So the course is like...
jf892iejf929j
{name: Course Name,
month1: date,
month2: date,
month3: date
}
Now, if the date of one of those months is, let's say today, I want to send an email. This is what I have right now.
const ia = _(courses)
.map((course, id) => ({id, ...course}))
.filter(course => course.course === 'Course Name')
.filter(course => moment(new Date(course.month9)).isSameOrAfter(moment(new Date())))
.map(course => {
if (moment(new Date(course.month1)).isSame(moment(new Date()))) {
HERE IS WHERE I'M TRYING TO AVOID 9 IF THENS
}
})
.value()
I'm trying to get something back like
{courseId: courseId,
coursetype: Course,
month: month1 (the original key,
date: date (original value of month1 key
}
I hope this makes sense... Is there an easier way to find just the months that match the date and then hand back data?
EDITED QUESTION FOR CLARITY
So, I found a local mentor here to walk me through this, and here is what we did.
Instead of iterating inside the lodash query, we returned the value
const ia = _(courses)
.map((course, id) => ({id, ...course}))
.filter(course => course.course === 'Course Name')
.filter(course => moment(new Date(course.month9)).tz("America/Phoenix").isSameOrAfter(moment(new Date(), 'day').tz("America/Phoenix")))
.value()
Then, we created the final object like this:
var finalObj = [];
for (var i = 0; i<ia.length; i++) {
for (var key in ia[i]) {
if (key.indexOf("month") >= 0) {
if (moment(new Date(ia[i [key])).tz("America/Phoenix").isSame(moment(new Date('Mon Jul 10 2017 20:00:00 GMT-0700 (MST)'), 'day').tz("America/Phoenix"))) {
finalObj.push({
"courseId": ia[i].id,
"courseName": ia[i].course,
"month": key,
"date": ia[i][key]
});
}
}
}
}
This did achieve the result I wanted, but I'm wondering if there isn't a way with ES6 and Lodash to achieve the same effect.