Skip to content

Instantly share code, notes, and snippets.

@mike-pete
Last active October 2, 2022 01:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-pete/6f22669ef91ffca4330361668090a1f8 to your computer and use it in GitHub Desktop.
Save mike-pete/6f22669ef91ffca4330361668090a1f8 to your computer and use it in GitHub Desktop.
const x = {
a:{
b:{
c:'neat'
}
}
}
const loop = (object, path) => {
let index = 0
let cur = object
while (index < path.length){
cur = cur[path[index]]
index++
}
return cur
}
console.log(loop(x, ['a','b','c']))
const x = {
a:{
b:{
c:'neat'
}
}
}
const loop = (object, path) => {
path = path.reverse()
let cur = object
while (path.length > 0){
cur = cur[path.pop()]
}
return cur
}
console.log(loop(x, ['a','b','c']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment