Skip to content

Instantly share code, notes, and snippets.

@sagarkbhatt
Created September 12, 2017 06:50
Show Gist options
  • Save sagarkbhatt/9877d0d55773e971788e6c7b1d371aa6 to your computer and use it in GitHub Desktop.
Save sagarkbhatt/9877d0d55773e971788e6c7b1d371aa6 to your computer and use it in GitHub Desktop.
js-path
function path(paths, obj) {
let val = obj;
let idx = 0;
while (idx < paths.length) {
if ( null == val ) {
return;
}
val = val[ paths[ idx ] ];
idx += 1;
}
return val;
}
let x = {
a: { b:
{ c: [ { x: 'sagar'}, {} ] }
}
};
let y = path( ['a','b','c',0,'x' ], x );
console.log( y );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment