title: JS - Efficient way to iterate throught lists inside lists
body: <p>What is the most efficient algorithm to find items inside another list? Let me try to write an example:</p>
  
  <ul>
  <li>I have the following structure:
  
  <ul>
  <li>Employers: <code>[{"id": 1, "skill": 10},{"id": 2, "skill": 90}];</code></li>
  <li>Customers: <code>[{"id": 12, "mood": 5},{"id": 2, "mood": 70}]</code>.</li>
  </ul></li>
  </ul>
  
  <p>That information is represented by an Array:</p>
  
  <ul>
  <li>Employers array: <code>[[1, 10], [2, 90]]</code>;</li>
  <li>Customers array: <code>[[12, 5], [2, 70]]</code>.</li>
  </ul>
  
  <p>In my case, employers can only interact with customers that has an <em>mood</em> lower than his <em>skill</em>. The return of my function should be the employer with highest interaction count.</p>
  
  <p>I've wrote a function that can do this rule but it's pretty slow when I have a big number of employers or customers - it took more than 7 seconds to finish.</p>
  
  <pre><code>function countEmployersByMoodAtt(operatos, customers) {
      let distribution = [];
      //sort operators by skill so I can guarantee that big skills won't come first
      operatos.sort((a, b) =&gt; a[1] - b[1]);
  
      operatos.forEach(cs =&gt; {
          let skill = cs[1];        
          //map to extract only the customer id
          let customersAvailable = customers
              .filter(customer =&gt; customer[1] &lt;= skill)
              .map(x =&gt; x[0]);
          //convert to set because i've wrote it's fast
          let customerAvSet = new Set(customersAvailable);
          //include on result list
          distribution.push([cs[0], customersAvailable.length]);
          //remove customers already assigned
          customers = customers.filter(([cs]) =&gt; !customerAvSet.has(cs));
      });
      //sort to first position be hightest score    
      distribution.sort((a, b) =&gt; b[1] - a[1]);
  
      //return operator
      return distribution[0][0];    
  }
  </code></pre>
  
  <p>An example input is:</p>
  
  <ul>
  <li><code>operators = [[1, 60], [3, 90]];</code></li>
  <li><code>customers = [[1, 30], [1, 40], [1, 60], [1, 70]];</code></li>
  </ul>
  
  <p>Output should be 1.</p>
  
  <p>Main rule: can't get the highest operator skill and put all on it. I need to balance between operators - I need to iterate from lower skill to higher one.</p>
  
  <p>Any tip on how can I can optimize it?</p>
  
  <p>Thanks in advance.</p>