title: iterating through object elements javascript
body: <p>I am trying to understand this:</p>
  
  <pre><code>  function keyValue(array) {
    var modeObj = {};
    var i=0;
    // create modeObj
    array.forEach(function(num){
  
      if (!modeObj[num]) {
        modeObj[num] = 0;
  
      } 
      modeObj[num]++;
  
       console.log("1 list object keys "+Object.keys(modeObj));
       console.log("2 list object value "+Object.values(modeObj));
      i++;
      console.log("3 iteration num "+i); 
    });  
  }
  </code></pre>
  
  <p>When iterating through an array [1,4,5,4,6,1], this is what is comming out:<br/></p>
  
  <pre><code>1 list object keys 1
  2 list object value 1
  3 iteration num 1
  1 list object keys 1,4
  2 list object value 1,1
  3 iteration num 2
  1 list object keys 1,4,52 list object value 1,1,1
  3 iteration num 31 list object keys 1,4,5
  2 list object value 1,2,1
  3 iteration num 4
  1 list object keys 1,4,5,62 list object value 1,2,1,1
  3 iteration num 5
  1 list object keys 1,4,5,6
  2 list object value 2,2,1,1
  3 iteration num 6
  </code></pre>
  
  <p>If found in the literature that "Object.keys method is used for returning enumerable properties of a simple array." As clear from an array [1,4,5,4,6,1] the key 1, and key 4 are twice in the array.  If we check the values of all corresponding keys we can see that the key-value represents a number of occurrences of partical key. Keys [1,4,5,6], the values - 2,2,1,1. </p>
  
  <p>In the first line, the new object is declared, then there is the iteration through the array which is sent into the function " keyValue()".  The array [1,4,5,4,6,1]. <br/><br/></p>
  
  <p>By the expression  "var modeObj = {};" just the empty object is declared. </p>
  
  <p>if(!modObj[element]) checks if the object's key (the element) in the iteration is undefined or has some value.  If the key does not exists the value 0 will be added as a value to the current key in the iteration. Later, it will be changed to the value 1 by expression "modeObj[num]++;"</p>
  
  <p>What I am not sure, is my interprettation of the expression <strong>modObj[element]++;</strong> I believe it could be interpreted in two staps:<br/></p>
  
  <p>The first step: <strong>modObj[element]= modObj[element]</strong>, <br/></p>
  
  <p>The second step: then <strong>modObj[element]=modObj[element]+1</strong><br/></p>
  
  <p>This means that <strong>modObj[element]</strong> will increment the value of a particular key, if the key is unique, will add just 1 which will represent a number of occurrences of the key, if that is the second or any other occurances of the key will add 1 to the existing number of occurrences.  </p>
  
  <p>Is that correct?</p>