I am looping some dates , adding them dynamically as properties to an object , and assigning a value to each. The properties are added but they all end up being set to the value assigned at the last iteration.
There is some referencing problem I guess. I'm not sure exactly where the problem is and what to do.
The below is also available in a fiddle with logging.
let dates = ['2020-04-28','2020-05-3','2020-05-16']
let pricesObj = {};
var entry = {
price: 45,
time_id: 2
}
dates.map( date => {
entry.date = date
if( !(date in pricesObj)){
//Add new date entry
pricesObj[date] = []
pricesObj[date].push(entry) //<-- this seems to be assigned to all dates, not just the current
}
})
console.log('updating price with obj: ' , JSON.stringify(pricesObj));
The logging shows the object with three date properties, and they all have the last "entry" with the wrong date.