Devmix
Devmix

Reputation: 1848

How to update an object property?

I have this array:

let myArray = [
  {
    id: 20,
    comments: {
      TICOL: 'This is a new comment'
    },
    result: 'my results'
  }
];

Now I'm trying to update the TICOL property by first creating a copy of the array like this:

let someArray = [...myArray];

let finalArray = someArray.map(obj => {
  obj.comments.TICOL = obj.comments.TICOL.replaceAll('new', 'TEST')
});

But finalArray is always [undefined]. Can anyone tell me what I'm missing? Thanks

Upvotes: 0

Views: 42

Answers (1)

andsilver
andsilver

Reputation: 5972

You have to return the new value within map method:

let finalArray = someArray.map(obj => {
   obj.comments.TICOL = obj.comments.TICOL.replaceAll('new', 'TEST');
   return obj;
});

Upvotes: 1

Related Questions