How to persist only a subset of environment variables programmatically after updating its value

Continuing the discussion from Persist environment variable automatically after updating it's value:

Programmatically update initial values from pre-request or test scripts

I tried the solution mentioned in the link above to be able to persist the current values to the initial values of environment variables programmatically. It works fine except that if I only wanted to update a subset of variables, the initial value of all the other variables that I did not want to update gets deleted.

For example, let’s say I have 10 different environment variables. In my script, I send a PUT request to the https://api.getpostman.com/environments/<environment_uid> and include only var1, var2, var3 to persist that current value to the initial values.

After the script runs, I see that the initial value of var1 to var3 gets updated correctly. But the initial value of var4 to var10 gets blanked out which is not my intention.

My question: Is there a way to do selective updating of initial values, without affecting all the other variables that I do not want to update?

I’ve done further research, and it seems that there is no direct way to persist current values to initial values for only a subset of variables. The only way I could address this is to get all the current environment variables, then update the values of the target subset. After which, I will pass the updated and complete set of environment to the Postman API to persist the values.

Here is an example of what I have created in Github Gist:

Postman - Persist Environment Variables