HDTuto.com

Angular Find and Update Object in Array Example



Hi

In this article we will cover on how to implement angular push element in array. I’m going to show you about angular push object into array. if you have question about angular object push key value then i will give simple example with solution. you will learn push object in array in angular. You just need to some step to done how to add element in array angular.

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...




April 26, 2020 | Category : Angular



Blog Search