title: Angular update object in object array
body: <p>Say i have the following object array, lets name it itemArray; </p>
  
  <pre><code>{
    "totalItems": 2,
    "items": [
      {
        "id": 1,
        "name": "foo"
  
      },
      {
        "id": 2,
        "name": "bar"
      },
      ]
  }
  </code></pre>
  
  <p>And i have a subscription that returns the updated result of only id 2. How would i update the object array without looping through the entire array?</p>
  
  <p>What i would like is something like the example below;</p>
  
  <pre><code>updateUser(user){
      this.myservice.getUpdate(user.id)
      .subscribe(newitem =&gt; {
        this.updateArray(newitem);
      });
  }
  
    updateArray(newitem){
      this.itemArray.items[newitem.id].name = newitem.name
    }
  </code></pre>
  
  <p>or even better, replacing the entire object;</p>
  
  <pre><code>  updateArray(newitem){
      this.itemArray.items[newitem.id] = newitem
    }
  </code></pre>
  
  <p>This example however updates the array based on the index of the array. So how do i instead update based on newitem.id?</p>
  
  <p>Template requested in comment:</p>
  
  <pre><code>&lt;tr *ngFor="let u of itemsArray.items; let i = index"&gt;
    &lt;td&gt;{{ u.id }}&lt;/td&gt;
    &lt;td&gt;{{ u.name }}&lt;/td&gt;
    &lt;td&gt;
      &lt;input type="checkbox" checked="u.accepted" [(ngModel)]="itemsArray.items[i].accepted" (ngModelChange)="updateUser(u)"&gt;
      &lt;label for="singleCheckbox-{{i}}"&gt;&lt;/label&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>