title: Comparing array value in javascript?
body: <p>I was working on my project where I had to compare some ID's and check if they are the same or not. First time I saw something like this </p>
  
  <pre><code>if (found[myRes[i].eventId] === 1) {
  </code></pre>
  
  <p>So I have array myRes that looks like this:</p>
  
  <pre><code>myRes.push({'eventId':42,'eventName':"Test 1",'rCustomer':"Mike, Allan"});
  
  myRes.push({'eventId':42,'eventName':"Test 1",'rCustomer':"Gates, Bill"});
  
  myRes.push({'eventId':19,'eventName':"Spring 2016",'rCustomer':"John, Bill"});
  
  myRes.push({'eventId':19,'eventName':"Spring 2016",'rCustomer':"Adams, Ron"});
  
  myRes.push({'eventId':31,'eventName':"May Test 1",'rCustomer':"Steve, Marie"});
  
  myRes.push({'eventId':42,'eventName':"Test 1",'rCustomer':"Ariel, Bill"});
  
  myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Ron, Nill"});
  
  myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Kim, Alen"});
  
  myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Will, Huges"});
  
  myRes.push({'eventId':32,'eventName':"Fall 2016",'rCustomer':"Seth, Peak"});
  </code></pre>
  
  <p>I have solution for my problem but I do not understand completely how it works. Here is the code that I use:</p>
  
  <pre><code>var found = [];
  
  for (var i = 0; i &lt; myRes.length; i++) {
      if (found[myRes[i].eventId] === 1) {
          $('#row_' + myRes[i].eventId).append('&lt;tr&gt;&lt;td&gt;'+myRes[i].some element+'&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;');
      } else {
          found[myRes[i].eventId] = 1;
          $('.myReservation').append('&lt;table&gt;&lt;tr&gt;&lt;/tr&gt;&lt;thead&gt;&lt;tr&gt;&lt;th &gt;'+myRes[i].eventName+'&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody id="row_' + myRes[i].eventId + '"&gt;&lt;tr&gt;&lt;td&gt;'+myRes[i].someelement+'&lt;/td&gt;&lt;/tr&gt;');
      }
  }
  </code></pre>
  
  <p>Here is the method that I used and did not work, this method outputted same event Name twice.</p>
  
  <pre><code>var eventId;
  
    for(var i=0; i&lt; myRes.length; i++){
      if(eventId === myRes[i].eventId){
        $('#row_' + myRes[i].eventId).append('&lt;td&gt;'+myRes[i].rCustomer+'&lt;/td&gt;');
      }else{
        eventId = myRes[i].eventId;
        $('.myReservation').append('&lt;tr id="row_'+myRes[i].eventId+'"&gt;&lt;td&gt;&lt;b&gt;Event: '+myRes[i].eventName+'&lt;/b&gt;&lt;/td&gt;&lt;td&gt;'+myRes[i].rCustomer+'&lt;/td&gt;&lt;/tr&gt;');
      }
    }
  </code></pre>
  
  <p>Output from code above:</p>
  
  <pre><code>Event: Test 1   Mike, Allan Gates, Bill
  Event: Spring 2016  John, Bill  Adams, Ron
  Event: May Test 1   Steve, Marie
  Event: Test 1   Ariel, Bill
  Event: Fall 2016    Ron, Nill   Kim, Alen   Will, Huges Seth, Peak
  </code></pre>
  
  <ol>
  <li><p>My question is what actually happens in my if statement? </p></li>
  <li><p>Does if statement basically looks for specific id that is assigned
  in found array and if does not exist in found array then left side
  of my if statement is 1 that will execute if statement? Or something
  else is behind that.</p></li>
  <li><p>Also in else statement new value is assigned to found array and set
  to 1. How that actually looks in found array then? Is that specific
  ID set to 1? </p></li>
  <li><p>If anyone can explain this to me I would appreciate. I tried to
  output found array to the screen but still that did not gave me
  enough to understand how this works. Thanks in advance.</p></li>
  </ol>