Reputation: 2002
I have this example, where I need to store several prices according to the day of the week and it's selected hour. This data comes as an Object and needs to be sent as a Map(), so the backend can work around with it. Unfortunately, it get's to a moment where it turns into an object. I think it's inside the const price
where that happens, but I need a better solution, so it never happens. How can I achieve this?
componentDidMount = async () => {
const dataFromDb = data.get('getting data here')
console.log('dataFromDb' ,dataFromDb) // Data from DB
let reservationPrice = new Map();
console.log('reservationPrice a Map()?', reservationPrice) // a Map();
console.log("Am I an object", dataFromDb.reservationPrice) // Object from DB
if (Object.entries(dataFromDb.reservationPrice).length === 0) {
console.log('entered') // not entering here
let maxHour = moment().startOf('day') // 00:00:00
let minHour = maxHour.clone().endOf('day') // 23:59:59
Object.keys(dataFromDb.schedule).map(key => { //getting schedule from DB
if (dataFromDb.operationSchedule[key] && moment(dataFromDb.operationSchedule[key].startTime, 'HH:mm') < minHour) {
minHour = moment(dataFromDb.operationSchedule[key].startTime, 'HH:mm')
}
if (dataFromDb.operationSchedule[key] && moment(dataFromDb.operationSchedule[key].endTime, 'HH:mm') > maxHour) {
maxHour = moment(dataFromDb.operationSchedule[key].endTime, 'HH:mm')
}
})
const hoursBetweenMinAndMax = moment.duration(maxHour.diff(minHour)).asHours()
let hours = new Map();
console.log('Am I a Map? hours', hours)
let auxHour;
for (let i = 0; i <= hoursBetweenMinAndMax; i++) {
auxHour = i !== 0 ? moment(minHour).add(i, 'hours') : moment(minHour)
hours.set(auxHour.format('HH:mm'), 0)
} // piece of code to get the min and max hour, then loop starting from the min hour and end until max hour: ex: 09:00, 10:00, 11:00...23:00
console.log('Am I a Map? hours', hours)
// let reservationPrice = new Map();
reservationPrice.set('monday', new Map(hours)) //shallow copy of hours
reservationPrice.set('tuesday', new Map(hours))
reservationPrice.set('wednesday', new Map(hours))
reservationPrice.set('thursday', new Map(hours))
reservationPrice.set('friday', new Map(hours))
reservationPrice.set('saturday', new Map(hours))
reservationPrice.set('sunday', new Map(hours))
console.log('reservationPrice', reservationPrice) // supposed to log with days and hours
console.log('Am I a Map()? reservationPrice', reservationPrice
instanceof Map) // A Map()
}
const price = Object.entries(dataFromDb.reservationPrice).length === 0 // dataFromDb.reservationPrice is an Object
? reservationPrice // reservationPrice is a Map();
: dataFromDb.reservationPrice // Here is turning an Object
console.log('Do I have days and hours?', price) // is an object, and not a Map();
console.log('Am I a Map()? price', price instanceof Map); //before: A Map() | //now: Now is an object;
this.renderFieldDetails(fieldId.id, price)
}
EDIT ---
If I do something like price = Object.entries(fieldInfo.reservationPrice).length !== 0 ? reservationPrice : fieldInfo.reservationPrice
It is turned into a Map(), but it is empty (in case there is already some data from DB) and on the onChange function bellow, error occurs from Map() being empty.
handlePriceInput = (e, hour, day) => {
let value = e.target.value
const newFieldReservationPrice = this.state.newFieldReservationPrice
console.log('newFieldReservationPrice is a Map()?', newFieldReservationPrice) //before: A Map() || now: not a Map() anymore
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 {
const newMap = new Map(Object.entries(newFieldReservationPrice))
console.log('newMap', newMap) // A Map()
map = new Map();
Object.keys(newFieldReservationPrice).forEach(key => {
map.set(key, new Map(Object.entries(newFieldReservationPrice[key])));
});
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('ggggg', isReservationPrice)
this.setState({
newFieldReservationPrice: isReservationPrice
})
}
Thank you! :)
Upvotes: 0
Views: 36
Reputation: 1
You can pass Object.entries()
with JavaScript plain object as parameter to Map()
constructor to create key, value pairs in a new Map
object
const o = {a:1, b:2, c:{d:3}};
const map = new Map(Object.entries(o));
console.log(map);
Upvotes: 1