Skip to content
Advertisement

Change property name from an array of object javascript/typescript

I have the following array of obect lets call it “myArray”:

JavaScript

What I would like to achieve is to switch the properties naming form name to key and the opposite so the final result is something like that:

JavaScript

I tried with

JavaScript

but its undefined and I tried also with

JavaScript

any suggestions? Thanks

Advertisement

Answer

You could handle it with a forEach:

JavaScript

It can technically work with map as well, but in general map should be used when you want to create a new array, and not to update in-place.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement