Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda