r/JavaScriptTips • • May 31 '22

Best way to transform object in JS

Hello all,

I have an object that needs to be transformed and I am not really sure how to approach it. The end result should also be an object with some difference:

- ConnString should be splitted into ConnString_Server which will have value tcp:myserver--- ConnString_InitialCatalog which will have value CRMTests, etc.

- All the nested values should be flattened, so I will need to have NeoLicense_Owner, NeoLicense_Key, SSO_Audience, etc

At the moment I am trying to loop through the object and do stuff based on the object keys.

What would you suggest as a best practice in this case? Is it better to handle everything in one function or it's better to try to handle in several smaller ones? I think I can find the solution that will work, so I don't want a finished solution, I would just like to get an advice on what's the best practice in this situation.

Thanks in advance!

const data = {
    "AlertName": "test reporting",
    "AlertEmail": "[email protected]",
    "AllowedHosts": "*",
    "Label": "Companies",
    "ConnString": "Server=tcp:myserver.database.windows.net,1433;Inital Catalog=CRMTest;Persist Security Info=False;User ID=testuser;Password=testuser@zz;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
    "DataEncryption": {
        "IV": "kdsdtyv8x/A?D(G+",
        "Key": "v9y/B?t7w!z%C&F)J",
    },
    "AdminDashboard": false,
    "Hangfire": {
        "WorkerCount": 10
    },
    "HangfireRedisPrefix": "test-prod",
    "NeoLicense": {
        "Owner": "Test Developer License",
        "Key": "C8QXP4LWRPG3QUI9PPY4T8A582SDRUEXPCFRGMA",
        "EditorOwner": "test - 1 WebApp LLC - Unlimited WebServer Lic",
        "EditorKey": "55756A2756949TRE593265EF1EF",
        "JSPMOwner": "test-OEM Developer License",
    },
    "OctToken": "95308651-0fd2-4baa-ace3-1ffa98a3dcc7",
    "RedisConnect": "northdev.redis.cache.windows.net:6381,password=okcz6oLA3piAzCaEuflYu,ssl=True,abortConnect=False",
    "SSO": {
        "Audience": "all",
        "BaseURL": "https://login-test.com",
        "ClientId": "test.com",
        "ClientSecret": "7a227fr-8f75-77-af70-th788",
    }
}
4 Upvotes

0 comments sorted by