const obj = {
2: 3,
very: {
nested: {
object: {
here: '!'
}
}
}
};
fromPath(obj, ''); // returns the obj itself
fromPath(obj, 'very.nested.object.here'); // '!'
fromPath(obj, 'very.nested.object'); // { here: '!' }
fromPath(obj, 2); // 3
fromPath(obj, 'very.bad.path.that.does.not.exist'); // undefined
fromPath(obj, ''); // returns the obj itself
const obj = {
2: 3,
very: {
nested: {
object: {
here: '!'
}
}
}
};
fromPath(obj, 2); // 3
assignToPath(obj, 2, 12); // obj[2] === 12 now
fromPath(obj, 2); // 12
assignToPath(obj, 'very.nested.object.here', '.');
fromPath(obj, 'very.nested.object.here'); // '.'
fromPath(obj, 'very.nested.object'); // { here: '.' }
assignToPath(obj, 'very.nested.object', '!');
fromPath(obj, 'very.nested.object'); // '!'
fromPath(obj, 'very.bad.path.that.does.not.exist'); // undefined
assignToPath(obj, 'very.bad.path.that.does.not.exist', 'really?');
console.log(obj); /* {
2: 3,
very: {
bad: {
path: {
that: {
does: {
not: {
exitst: 'really?'
}
}
}
}
},
nested: {
object: '!'
}
}
} */