title: JavaScript: Can a nested object value reference its parent object key?
body: <p>I have an object as follows:</p>
  
  <pre><code>var obj = {
      parentKey : {
          nestedKey1 : value, 
          nestedKey2 : function () {
              return parentKey + nestedKey2;
          }
      }
  };
  </code></pre>
  
  <p>Is there a way to use the values within the actual name of the <code>parentKey</code> and <code>nestedKey1</code> and/or <code>nestedKey2</code> in a function held in one of the nested key-value pairs as the one above? </p>
  
  <p>In my actual scenario the <code>keys</code> are all numerical values I wish to use as criteria for a <code>for loop</code>.</p>
  
  <hr>
  
  <h2>Updated Scenario To Question</h2>
  
  <p>As an exercise to learn JavaScript I decided to code the logic of an elevator system. To do this I have made an object which represents the building and this object contains each floor, nested within each floor are the data of the number of people wishing to travel to another floor as follows:</p>
  
  <pre><code>var floorData = {
      &lt;floor number&gt; : {
          &lt;people going to floor X&gt; : &lt;number of people&gt;,
          &lt;people going to floor Y&gt; : &lt;number of people&gt;,
          peopleGoingUp : &lt;formula that sums amount of people going up&gt;,
          peopleGoingDown : &lt;formula that sums amount of people going down&gt;
      }
  };
  </code></pre>
  
  <p>I want to include within each <code>&lt;floor number&gt;</code> object a property that sums the amount of <code>peopleGoingUp</code> and <code>peopleGoingDown</code> by means of a formula. For this formula to work as I intend I need to access the <code>&lt;floor number&gt;</code> name which is a numeric value from within the <code>peopleGoingUp</code> and <code>peopleGoingDown</code> formulas.</p>
  
  <p>Here is my working example thus far and I have included <code>peopleGoingUp</code> and <code>peopleGoingDown</code> formulas in floor <code>theBuilding[2]</code>. What I wish for is to change the hand entered value of <code>theParentKey</code> to equal the name of the parent object.</p>
  
  <pre><code>// Total number of floors in building global object
  var totalFloors = 3;
  
  // Building object including amount of people waiting to take lift to other floors on each level of the building
  var theBuilding = {
      0 : {
          1 : 2,
          2 : 0,
          3 : 1,
      },
      1 : {
          0 : 1,
          2 : 3,
          3 : 2,
      },
      2: {
          0 : 2,
          1 : 1,
          3 : 4,
          goingUp : function () {
              var sum = 0;
              var theParentKey = 2; // this is supposed to be a direct reference to the parent object name to this nested object and thus equal to 2
              for (i = 0; i &lt;= totalFloors; i++) { // loop for total floors 
                  if (i !== theParentKey &amp;&amp; i &gt;= theParentKey) {//sum if i isn't = this floor number and that i is greater than this floor number
                      sum += this[i]
                  }
              };
              return sum;
          },
          goingDown : function () {
              var sum = 0;
              var theParentKey = 2; // this is supposed to be a direct reference to the parent object name to this nested object and thus equal to 2
              for (i = 0; i &lt;= totalFloors; i++) { // loop for total floors from lowest
                  if (i !== theParentKey &amp;&amp; i &lt;= theParentKey) { //sum if i isn't = this floor number and that i is less than this floor number
                      sum += this[i]
                  }
              };
              return sum;
      },
      3 : {
          0 : 0,
          1 : 1,
          2 : 4
      }
  };
  
  console.log(theBuilding[2].goingUp()); // 4
  console.log(theBuilding[2].goingDown()); // 3
  </code></pre>