0๐
โ
So basically dates should be strings in first place. Next probably incorrect place is where you use [_.map(arr, 'date')]
which actually place result of map into array making it double array. Also in order to parse arr
into Date
objects you need map arr values to parseISO
function. So following code should work
var closestIndexTo = require('date-fns/closestIndexTo')
var parseISO = require('date-fns/parseISO')
var _ = require('lodash')
val = "2019-10-04"
arrDates = ["2019-09-01","2019-09-03","2019-09-03","2019-09-04","2019-09-05","2019-09-05","2019-09-23","2019-10-01","2019-11-18"]
function getClosestToDate(val, arr) {
var arrDates = _.map(arr, (a) => parseISO(a))
var closestDate = closestIndexTo(parseISO(val), arrDates)
return closestDate
}
console.log("result", getClosestToDate(val, arrDates))
Here is link to repl
Source:stackexchange.com