I'm facing an issue with the code below and cannot figure out why I am encountering the error message. I have ensured that each object contains a value, so why is there a reference to 'undefined'?
Cannot set property 'two' of undefined
interface FakeObj {
one: string | null;
two: string | null;
three: string | null;
}
const objectOne: FakeObj[] = [
{
one: null,
two: "Two",
three: null
}
];
const objectTwo: FakeObj[] = [
{
one: "One",
two: null,
three: "Three"
}
];
const newObject = objectOne.map((search, index) => {
return Object.entries(search).reduce((acc, [key, value]) => {
acc[key] = !value && objectTwo.length > index ? objectTwo[index][key] : value;
}), {};
});
console.log(newObject);
UPDATED: After updating the code, I encountered new issues:
Cannot set property 'two' of undefined
Cannot create property 'two' on string 'One'