jawdat abdallh
jawdat abdallh

Reputation: 106

How to update an item of a list with angular

I'm trying to update a model of type 'SomeModel' in a list of elements of type SomeModel[]

I'm trying to point to the object needed as follow:

onChange(args: SomeModel): void {
  let element: SomeModel = this.elements.find(e => e.name === args.name);
  console.log(element) // <-- this prints the right object from the list
  element = JSON.parse(JSON.stringify(args)); // <-- isn't working

}

What I'm doing wrong?

Upvotes: 0

Views: 2606

Answers (2)

jawdat abdallh
jawdat abdallh

Reputation: 106

thanks guys, the complete answer is:

onChange(args: SomeModel) void {
this.elements = this.elements.filter(e => e).map(e => e.name === args.name ? args : e);

Upvotes: 0

SeleM
SeleM

Reputation: 9678

Try with:

onChange(args: SomeModel): void {
  this.elements = this.elements.map(e => {
    if (e.name === args.name){
      return args;
     } 
    return e;
   })
}

Upvotes: 1

Related Questions