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

194
Vistas
Property assignment expected when using template literals to create an Object

I am trying to create an object inside a JSON object that will be later populated from FormData:

function toJSONstring(form) {

  let object = {`"${form.name}": {}`}; //Property assignment expected.ts(1136)

  let formData = new FormData(form);
  formData.forEach((value, key) => {
    // Reflect.has in favor of: object.hasOwnProperty(key)
    if (!Reflect.has(object, key)) {
      object[key] = value;
      return;
    }
    if (!Array.isArray(object[key])) {
      object[key] = [object[key]];
    }
    object[key].push(value);
  });
  form.reset();
  return JSON.stringify(object);
}

What I want to obtain is the following JSON:

{
    "network_settings": {
        "connection": "wifi",
        "type": "dhcp",
        "ssid": "Jorj_2.4",
        "ip_address": "",
        "gateway": "",
        "subnet": "",
        "dns": ""
    }
}

but I get Property assignment expected.ts(1136).

If I replace

let object = {`"${form.name}": {}`};

with let object = {}; then I get a normal JSON object:

{
    "connection": "wifi",
    "type": "dhcp",
    "ssid": "Jorj_2.4",
    "ip_address": "",
    "gateway": "",
    "subnet": "",
    "dns": ""
}

How can I created the nested object that I want ?

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

0

For dynamic keys in script you have to enclose your expression inside []

const form = {
  name: 'network_settings'
}
let obj = {[form.name]: {}};
console.log(obj);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Go back to what you had first:

let object = {};

And after you have populated the object, wrap it using computed property name syntax:

return JSON.stringify({ [form.name]: 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