Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
Imbricated json object value change

I have an empty javascript object, and I want to do something like this:

this.conflictDetails[this.instanceId][this.modelName][this.entityId] = { solved: true };

The problem is that for the modelName I get Cannot read properties of undefined. I think it is because the empty object this.conflictDetails does not have the property that I m looking for, I wonder how I could write this to work and to be as clear as possible?

The result should look like:

{
  'someInstanceId': {
    'someModelName': {
      'some entityId': {
        solved: true;
      }
    }
  }
}

With mu; time instance ids, that hold multiple model names, that have multiple entity Ids

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

check this the '?' operator

let obj = {
  'someInstanceId': {
    'someModelName': {
      'someEntityId': {
        solved: true
      }
    }
  }
};

let value1 = obj.someInstanceId?.someModelName?.someEntityId;
console.log(value1);

// or
value1 = obj["someInstanceId"] ?? {};
value1 = value1["someModelName"] ?? {};
value1 = value1["someEntityId"];
console.log(value1);

let value2 = obj.someInstanceId?.someMissModelName?.someEntityId;
console.log(value2);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use interfaces to define the model of the object, or you can check if every nested object is not null nor undefined.

I would prefer the first option if I use typescript. If not, then i would stick to the not null/undefined option

First we create a model:

export interface CustomObject {
  [instanceId: string]: {
    [modelName: string]: {
      [entityId: string]: {
        solved: boolean;
      };
    };
  };
}

Now that we have a model we can fill an object with data:

We define the object in the .ts file of your component

public dataObjects: CustomObject = {
  instance1: {
    basicModel: {
      Entity1: {
        solved: false,
      },
    },
  }
};

Now you can have for example a function to change the solved status, I assume that there are several instanceIds, modelNames and entityIds.

public resolveObject(
  instanceId: string,
  modelName: string,
  entity: string,
  solved: boolean
) {
  this.dataObjects[instanceId][modelName][entity].solved = solved;
}

Now you can call the method with the object details:

this.resolveObject('instance1', 'basicModel', 'Entity1', true);

After this line is executed the solved will changer from false to true.

I've created a StackBlitz to show you: https://stackblitz.com/edit/angular-ivy-ibfohk

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda