---
title: "Path Resolver with JavaScript Proxies"
description: "Using ES6 Proxies to access nested object paths with dot notation strings. Practical notes and code examples on Path in JS."
url: "https://h3manth.com/scribe/path-resolver-with-javascript-proxies/"
canonical_url: "https://h3manth.com/scribe/path-resolver-with-javascript-proxies/"
markdown_url: "https://h3manth.com/scribe/blog/path-resolver-with-javascript-proxies.md"
publishDate: "2016-06-15T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript","es6","proxy"]
word_count: 78
reading_time_minutes: 1
x_markdown_tokens: 207
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Path Resolver with JavaScript Proxies

> Using ES6 Proxies to access nested object paths with dot notation strings. Practical notes and code examples on Path in JS.

A question from #javascript on freenode: How to access `foo['x']['y']['z']` using `foo['x.y.z']`?

## Solution with Proxies

```javascript
var handler = {
  get: function(target, path) {
    return path.split(".").reduce(
      (o, k) => o && (k in o) ? o[k] : undefined,
      target
    );
  }
};

var obj = new Proxy({}, handler);

Now you can do:
javascript
obj['a'] = { b: { c: 1 } };
obj['a.b.c']; // 1
obj['a.x.c']; // undefined (safe!)
```

Just for the fun of it! 🎉
