Created
September 12, 2017 06:50
-
-
Save sagarkbhatt/9877d0d55773e971788e6c7b1d371aa6 to your computer and use it in GitHub Desktop.
js-path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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