I wonder if anyone could help me.
I have an array of dates, which looks like
dates_array_a = ["2021-12-07","2021-12-08","2021-12-09","2021-12-10","2021-12-11","2021-12-12","2021-12-13","2021-12-14"];
I then have another set of array dates, but this time they have a numeric value
dates_array_b = [ ['2021-12-07', 100], ['2021-12-10', 555], ['2021-12-13', 750] ];
How can I do a for loop on "dates_array_a" and then look for any matches in "dates_array_b", so it creates the following;
2021-12-07 100
2021-12-08 100
2021-12-09 100
2021-12-10 555
2021-12-11 555
2021-12-12 555
2021-12-13 750
2021-12-14 750
I basically want to run a for loop, then create a fresh array from the results using push
I would post my inital code, but I have no idea where to start
Any help or advice would be really appriciated
May be this code can help you:
dates_array_a = ["2021-12-07","2021-12-08","2021-12-09","2021-12-10","2021-12-11","2021-12-12","2021-12-13","2021-12-14"];
dates_array_b = [ ['2021-12-07', 100], ['2021-12-10', 555], ['2021-12-13', 750] ];
let lastKnownKey;
const result = dates_array_a.map((dateA) => {
const [date, key] = dates_array_b.find(([dateB]) => dateB === dateA) ?? [null, null]
lastKnownKey = key ?? lastKnownKey;
return `${dateA} ${lastKnownKey}`;
});
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}