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

190
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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