//console.log(getByPath(obj, "")); // Должно вывести undefined или объект целиком в зависимости от реализации
const obj = { inner: { test: { value: 'hello world' } } };
const obj2 = { users: [{ name: "Alice" }, { name: "Bob" }] };
const obj3 = { "hello world": { value: 42 } };
const obj4 = { inner: null };
const obj5 = { inner: null };
 
const obj6 = {};
obj6.self = obj6;
 
const path = 'inner.test.value';
const path2 = 'inner.test';
const path3 = 'inner.nonExistentKey.something'
const path4 = ''
const path5 = 'users.1.name'
const path6 = 'inner'
 
const getByPath = (obj, path) => {
const result = path.split('.').reduce((obj, key)=>{
return obj ? obj[key] : undefined
}, obj)
return path === '' ? undefined : result
}
 
console.log(getByPath(obj6, "self.self.self"));
 
console.log(getByPath(obj, "inner.nonExistentKey")); // Должно вывести undefined
 
console.log(getByPath(obj, "")); // Должно вывести undefined или объект целиком в зависимости от реализации
 
Click Run to execute JavaScript.
Code runs in an isolated environment.