I have a Map() object that I need to iterate, so I can get the day of the week and a selected hour. The code below doesn't work, because Object.keys(newFieldReservationPrice).forEach
is trying to loop a Map() object, which seems to make no sense. So, is there a better solution for this?
Here is the code below:
handlePriceInput = (e, hour, day) => {
let value = e.target.value
const newFieldReservationPrice = this.state.newFieldReservationPrice
console.log('newFieldReservationPrice', newFieldReservationPrice) // A Map();
let map;
if (!newFieldReservationPrice instanceof Map) {
console.log('!== Map')
console.log('newFieldReservationPrice is a Map()? inside if ()', newFieldReservationPrice)
if (newFieldReservationPrice[day] && newFieldReservationPrice[day][hour]) {
newFieldReservationPrice[day][hour] = Number(value)
}
} else {
map = new Map();
console.log('map object', Object.keys(newFieldReservationPrice)) // logs map object []
Object.keys(newFieldReservationPrice).forEach(key => {
console.log('key', key)
map.set(key, new Map(Object.entries(newFieldReservationPrice[key])));
}); // This doesn't work
console.log('Am I a Map()? map', map)
const aux = map.get(day)
console.log('aux day', aux) // A Map()
aux.set(hour, Number(value)) // Comes as undefined || Cannot read property 'set' of undefined
console.log('aux set', aux) // A Map()
map.set(day, aux);
console.log('what do I have?', map)
}
const isReservationPrice = !newFieldReservationPrice instanceof Map ? newFieldReservationPrice : map
console.log('isReservationPrice', isReservationPrice)
this.setState({
newFieldReservationPrice: isReservationPrice
})
}
Thank you! :)