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

153
Views
How to use the value of a variable as a property of an object?

In the example below, the object doesn't seem to understand which category is a variable and so it uses as a string:

var categoria = "Carros";
var obj       = {
    categorias: {}
};
obj.categorias = {...obj.categorias, categoria: {}}
console.log(obj.categorias)

I want the category property to be named "Cars" and not category. I tried it the way below, but it returns a template string error:

var categoria = "Carros";
var obj       = {
    categorias: {}
};
obj.categorias = {...obj.categorias, `${categoria}`: {}}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use square brackets!

const categoria = "Carros";

const obj = {
  categorias: {}
};

obj.categorias = { ...obj.categorias, [categoria]: {} };

console.log(obj.categorias);

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!