I'm currently working on a loop within my code that involves adding dates from an array (dates) as key/value pairs ("date":"dates[i]") to objects in another array (values).
values.forEach((obj, i) => obj.date = dates[i]);
The issue arises when some elements in the 'values' array are null and I encounter an error message saying 'ERROR TypeError: Cannot set property 'date' of null.'
Is there a way to handle this problem using TypeScript? Can I implement a check or condition for these null values and replace them with a new object containing the missing key/value pair ("date":"dates[i]")?
For example:
values.forEach((obj, i) =>
if (values[i] == null){
values[i] = {}
values[i].date = dates[i]
}
else {
obj.date = dates[i]
}
);
In case it's relevant, I am using Angular 5 components for implementing this logic, hence technically referring to them as this.values and this.dates.
I'm struggling to find a solution to this issue. Thank you for your help!