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

184
Vistas
Is there any way to dynamically add JSON fields in Javascript?
const demo = {
      data:[]
    };    
this.state.lossGroup.forEach(element => {
          const z= []
          this.state.selectedExposure.map((e) => {
            console.log(element,e.hazard_index.type )
            if (element == e.hazard_index.type) {
              z.push(e.id)
            }
          })
           demo.data.push({ element: z });
        });

I just want to store the array in json file with dynamic field name in element i.e. element should be the replaced with the value from the upper loop.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If your key is dynamic they you can do this way. [element] is way you can handle dynamic keys.

const demo = {
      data:[]
    };    
this.state.lossGroup.forEach(element => {
          const z= []
          this.state.selectedExposure.map((e) => {
            console.log(element,e.hazard_index.type )
            if (element == e.hazard_index.type) {
              z.push(e.id)
            }
          })
           demo.data.push({ [element] : z });
        });

here you can go through small blog which explains how you can set dynamic keys to json object.

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