Skip to content
Advertisement

Typescript map specific columns from an array

I have an array of objects and I need a way to allow the user to select which properties they want to import in the database. Is there a way to map and create a separate array only with the properties the user actually wants to send insert.

For example, if we have the following array:

JavaScript

and the user selects name and phone only, then the array that is sent to be added in the database should look like this:

JavaScript

How can this be achieved?

Advertisement

Answer

Use map and return the new object

JavaScript

If you want to make it dynamic with an array of props to copy over

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