Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!