Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getPropValueByStringPath = (target, str) => {
- const path = str.split('.');
- const key = path.shift();
- if(path.length !== 0 && target[key] instanceof Object)
- return getPropValueByStringPath(target[key], path.join('.'));
- else if(path.length === 0)
- return target[key];
- }
- // example
- const target = {
- a: {
- b: {
- c: {
- d: 8
- }
- }
- }
- }
- getPropValueByStringPath(target, 'a.b.c.d'); //returns 8
Add Comment
Please, Sign In to add comment