`const optionalChaining = (obj, chain, result) => {
let value = chain[0];
if (!obj.hasOwnProperty(value)) {
return;
} else if (
typeof obj[value] === 'object' &&
obj[value] !== null &&
chain.length > 1
) {
chain.shift();
result = optionalChaining(obj[value], chain);
} else {
result = obj[value];
result = optionalChaining(obj[value], chain);
}
return result;
};
const obj = {
a: {
b: {
c: {
d: 'Привет',
},
},
},
};
const arr = ['a', 'b', 'c', 'd'];
console.log(optionalChaining(obj, arr));`