Guest User

Untitled

a guest
Apr 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. const getPropValueByStringPath = (target, str) => {
  2. const path = str.split('.');
  3. const key = path.shift();
  4.  
  5. if(path.length !== 0 && target[key] instanceof Object)
  6. return getPropValueByStringPath(target[key], path.join('.'));
  7. else if(path.length === 0)
  8. return target[key];
  9. }
  10.  
  11. // example
  12. const target = {
  13. a: {
  14. b: {
  15. c: {
  16. d: 8
  17. }
  18. }
  19. }
  20. }
  21.  
  22. getPropValueByStringPath(target, 'a.b.c.d'); //returns 8
Add Comment
Please, Sign In to add comment