printing array shows different values when printing only the last element - javascript

Viewed 75

im getting logs with a date property in the ISO YYYYMMDDhhmmss format, and im transfering them to Date object using the map function as seen below.

when I print the whole object I get the wrong dates but when I print just one element of the array its correct, this is bizarre to me,what am I missing ?

  let dates = ascendingLogs.map(log =>  new Date(log['date'].replace(
      /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
      '$4:$5:$6 $2/$3/$1'
  )));


  console.log(dates[dates.length - 1])
  console.log(dates)


  let numberOfElements = 8
  let timeGap = (dates[dates.length -1].getTime() - dates[0].getTime()) / numberOfElements;
  let labels = []
 
    for (let i = 1; i < numberOfElements; i ++){
      dates[i] = new  Date(dates[i - 1].getTime() + (timeGap));
      
    };

below are the console.log of 1. last element in dates, whole dates array, ascendingLogs object

printscreen of the console

ascending logs data

EDIT : I have been able to narrow down the problem to the for loop, what im thinking is that it somehow asynchronously changes the console.log value but I dont know how to solve it.

heres a reproducible example you could examine :

0 Answers
Related