I'm working with a function that needs to return an array with data, the data of the Dates is always the same but the definition of the dates is not the same, here the dates saved in an array, every date is different but when I try to put this data in other array always return the last date from the array:
const dates=[new Date(startTimestamp), new Date(startTimestamp+ 6.048e+8), new Date(startTimestamp+2*+ 6.048e+8), new Date(startTimestamp+3*+ 6.048e+8)]
for (let index = 0; index < 4; index++) {
const week = weeksData[weekIndex + index];//I have an array of weeks to search for some data
//if that data is not in the array then i want to add a new week with a new Date
if (week !== undefined ) {
newWeeks.push(week);
}
else{
const mockWeek={}
mockWeek.timestamp=dates[index].valueOf();
mockWeek.week= dayjs(dates[index]).format('MMM DD');
console.log(dates[index])
console.log(mockWeek)
newWeeks.push(mockWeek)
}
}
What I'm doing wrong ?
The weeks array is like this
Array(17)
0: Week
id: "test-project_1652590800000"
projectId: "test-project"
data:{...}
timestamp: 1652590800000
week: "May 15"
1: Week
id: "test-project_1641196800000"
projectId: "test-project"
data:{...}
timestamp: 1641196800000
week: "Jan 03"
2: Week
id: "test-project_1641801600000"
projectId: "test-project"
data:{...}
timestamp: 1641801600000
week: "Jan 10"
I search in this array by some Date, but if the date don´t exist, thats when I want to add the mockWeek, with the same data,but with a different week and timestamp, there's when the mock week always have the last date from the array, here i don't have the may 8 and may 1 for example, then i want to add a mock week like this
3: Week
id: "test-project_1651381200000"
projectId: "test-project"
data:{...}
timestamp: 1651381200000
week: "May 1"
4: Week
id: "test-project_1651986000000"
projectId: "test-project"
data:{...}
timestamp: 1651986000000
week: "May 8"
But instead I have in both week data the "May 8" and timestamp from "May 8"
This isn't an answer, it's a long comment seeking clarification.
Creating a runnable snippet from the posted code shows it doesn't behave as described. I've added semicolons and fixed the syntax errors. It seems to do what you want (i.e. add missing week objects to the newWeeks array).
You need to post code (preferably as a runnable snippet) that demonstrates the issue. Until then, responses can only be guesses.
let startTimestamp = new Date().setHours(0,0,0,0);
let dates=[new Date(startTimestamp), new Date(startTimestamp+ 6.048e+8), new Date(startTimestamp+2*+ 6.048e+8), new Date(startTimestamp+3*+ 6.048e+8)];
let weekIndex = 5; // Ensure no weeks are found
let newWeeks = [];
let weeksData = [{ // Minimal data in array from OP
0: 'Week',
timestamp: 1652590800000,
week: "May 15"
},{
1: 'Week',
timestamp: 1641196800000,
week: "Jan 03"
},{
2: 'Week',
timestamp: 1641801600000,
week: "Jan 10"
}];
// Loop from OP
for (let index = 0; index < 4; index++) {
let week = weeksData[weekIndex + index];
if (week !== undefined ) {
newWeeks.push(week);
} else {
let mockWeek = {};
mockWeek.timestamp = dates[index].valueOf();
mockWeek.week = dayjs(dates[index]).format('MMM DD');
console.log(dates[index]);
console.log(mockWeek);
newWeeks.push(mockWeek);
}
}
console.log(newWeeks);
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.2/dayjs.min.js"></script>