gpt4 book ai didi

javascript - 将对象从 DB 转换为 Map()

转载 作者:行者123 更新时间:2023-12-03 00:16:37 25 4
gpt4 key购买 nike

我有这个例子,我需要根据星期几和选定的时间存储几个价格。该数据作为对象出现,需要作为 Map() 发送,以便后端可以使用它。不幸的是,它到了变成一个物体的时刻。我认为这是在 const Price 内发生的,但我需要一个更好的解决方案,所以它永远不会发生。我怎样才能实现这个目标?

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)
}

编辑---

如果我做类似 price = Object.entries(fieldInfo.reservationPrice).length !== 0 的事情?预订价格:fieldInfo.reservationPrice

它变成了一个Map(),但它是空的(如果数据库中已经有一些数据),并且在下面的onChange函数中,由于Map()为空而发生错误。

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
})
}

谢谢! :)

最佳答案

您可以将 Object.entries() 与 JavaScript 普通对象作为参数传递给 Map() 构造函数,以在新的 Map< 中创建键、值对 对象

const o = {a:1, b:2, c:{d:3}};

const map = new Map(Object.entries(o));

console.log(map);

关于javascript - 将对象从 DB 转换为 Map(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54508380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com