I am trying to modify the values of an object by using .fromEntries()
.
As you can see in the picture below, I am literally returning a modified Array of hours, but after the function ends, it returns the complete Array, instead of the updated one.
Thank you for any ideas in advance!
let filteredDates = Object.fromEntries(Object.entries(slots).filter(([slot, value]) => {
let result = datesArray.some(filterdates => {
return filterdates.some(filter => slot === filter)
})
let currentDay = slot === day ? value.filter(time => time >= currentHour) : null
result = currentDay ? currentDay : result ? value : result
console.log("result",result)
return result
}))
console.log("filteredDates",filteredDates)
I think the closest solution to your current approach is to
map
first, returningnull
for the entries you want to delete, and thenfilter
out thenull
entries to preventObject.fromEntries
from throwing. In code:If you want to do it in one pass, you could skip
Object.fromEntries
andreduce
the entries in to an object yourself: