Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. function getIn(o, path) {
  2. if (path.length === 0) return o
  3. if (typeof o !== 'object') return
  4. return get(o[path[0]], path.slice(1, path.length))
  5. }
  6. function setIn(o, path, v) {
  7. let count = 0
  8. let parent = o
  9. while (count < path.length) {
  10. const current = getIn(o, path.slice(0, count + 1))
  11. if (typeof current === 'object') {
  12. parent = current
  13. } else {
  14. parent[path[count]] = {}
  15. parent = parent[path[count]]
  16. }
  17. count++
  18. }
  19. parent[path[count - 1]] = v
  20. return o
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement