Angular - How to Find and Update Object in Array?

By Hardik Savani October 20, 2023 Category : Angular

Hi

This tutorial will give you example of angular find and update object in array. step by step explain angular update object in object array. This article goes in detailed on angular update array of objects. you can see angular update object in array.

Here, i will give you very simple example of how to find object from object array and we will update object value in array.

So,let's see simple example that will help you.

src/app/app.component.html

<button *ngFor="let item of myObjArray" (click)="updateItem(item)">Update</button>

src/app/app.component.ts

import { Component } from '@angular/core';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular';

myObjArray = [

{id: 1, name: "Hardik" },

{id: 2, name: "Vimal" },

{id: 3, name: "Paresh" }

];

updateItem(item){

let index = this.myObjArray.indexOf(item);

item.name = "Change Hardik";

this.myObjArray[index] = item;

console.log(this.myObjArray);

}

}

Output:

[Object, Object, Object]

0: Object

id: 1

name: "Change Hardik"

__proto__: Object

1: Object

2: Object

I hope it can help you...

Shares