title: Typescript. How to get object's property name from its value?
body: <p>I'm writing a typescript class for using with <a href="http://facebook.github.io/immutable-js/docs/#/Map/setIn" rel="nofollow">immutable map</a></p>
  
  <pre><code>class NavigableObject&lt;T&gt; {
      constructor(private obj: T, private path: string[] = []) { }
  
      To&lt;R&gt;(p: (x: T) =&gt; R): NavigableObject&lt;R&gt; {
          return new NavigableObject&lt;R&gt;(p(this.obj),
                         this.path.concat(this.getPropName(p(this.obj))));
      }
  
      getPath() {
          return this.path;
      }
  
      private getPropName(value) {
          for (var item in this.obj) {
              if (this.obj[item] === value) {
                  return item;
              }
          }
      }
  }
  
  let test = {
      a: {
          a1: 1,
          a2: 1
      },
      b: {
          b1: 1,
          b2: 2
      }
  }
  
  let navigableTest = new NavigableObject(test);
  
  navigableTest.To(m =&gt; m.b).To(m =&gt; m.b2).getPath(); // = ["b", "b2"]
  
  navigableTest.To(m =&gt; m.a).To(m =&gt; m.a2).getPath(); // = ["a", "a1"] &lt;-- I selected a2, tho
  </code></pre>
  
  <p>There is a problem with <code>getPropName</code> method. When <code>obj</code> has two properties with same value, only the first property will be matched.</p>
  
  <p><strong>Does anyone know how to work around this?</strong></p>