Need help to loop into a obj and find missing key

Hi all, I am doing API Call and getting a huge response that I am splitting etc… to get a key value pair. I am storing everything in a local variable ‘obj’.

The response once everything is splitted, look like this :

{
	{

	"key1" = "key1value"

	},

	{

	"key2" = "key2value"

	},

	{

	"var.var2[abcd-def].url" = "urlabcd"

	},

	{

	"var1.var3[ertty-def].url" = "fewfew"

	}
}

etc…

Sometimes the key itself is missing so if that the case I would need to call another API to get the value in a different file that I am putting in a local variable directly on Init.

KeyMissing = {}

For now my TS file is very huge because I am doing a check 1 by one like :

if (!this.obj['var.var2[abcd-def].url']) {

this.environment.abcd= this.KeyMissing['var.var2[abcd-def].url'];

} else {

this.environment.abcd= this.obj['var.var2[abcd-def].url'];

}

if (!this.obj.key1) {

this.environment.abcd= this.KeyMissing.key1;

} else {

this.environment.abcd= this.obj.key1;

}

etc…

I have like 60 if/else.

Is there any way to do it in 1 line or shorter? If so could you please provide exemple with the data shown above.

I think about something like making an array of the needed key that I want to print out on my html file:

  arrayNeeded = ['client.endpoints[party].url', 'client.endpoints[enrolment].url'] 

And then loop into the obj to see if a key is missing but giving its not working:

for (var key of Object.keys(this.obj)) {
        if(!this.arrayNeeded.find(k => k === this.obj[key])  &&  this.envName.includes('sys')) {

          this.environment.party = this.objMissingSYS[key]

        }else if(!this.arrayNeeded.find(k => k === this.obj[key])  &&  this.envName.includes('dev'))  {

          this.environment.party = this.objMissingDEV[key]

        }else {

          this.environment.party = this.obj[key]

      }

    }

2 suggestions:

  1. Try asking in stackoverflow
  2. Any site where you post code, will ask you to do an MWE this means Minimal Working Example (or Minimal Not Working example) reproducing the issue.

Right now, I doubt anyone will be keen to read those blocks of code.


In case it helps, there is a way to loop over objects like this:

for (const property in object){
//do smthj
}

Check MDN for good examples.