title: Angular2 Observable.map capture value of local variable
body: <p>How can one capture <em>value</em> of a local variable for use inside <code>Observable.map()</code> callback? For example, in my Anglar2 application I would love to capture <em>value</em> of <code>quantity</code> for use in <code>findItem().map()</code>:</p>
  
  <pre><code>let quantity : number = 1; //&lt;-- needs to be captured
  let ordered : string[] = [];
  //not interested in actual results, just adding queried items to cart
  let queries : Observable&lt;void&gt;[] = [];
  //res.entities are NLP classified entities, either item_id:string or quantity:number
  for (let entity of res.entities) {
      if(entity.entity == 'item') {
          queries.push(
              this.findItem(entity.value).map(item =&gt; {
              if(item != null)
              {
                  for(let i : number = quantity; i &gt; 0; i--) this.cart.addItem(item);
              }
              ordered.push(quantity + 'x ' + item.fullName);//NL message for user
          }));
          quantity = 1;//reset quantity
      }
      else if(entity.entity == 'quantity') quantity = entity.value;
  }
  return Observable.forkJoin(queries, ...);
  </code></pre>
  
  <p>The <code>ordered</code> will show quantity 1 for all items (because value of <code>quantity</code> is always 1 at the end of the loop). <br>
  My research shows that this is very common problem in JavaScript, and there are plenty of examples how to capture a variable in <code>for</code> loops. I wasn't able, however, to find any information how to capture variable values for use in <em>callback</em>s, such as <code>Observable.map()</code></p>