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

154
Views
Using a variable for a property name

When I try this code, I cannot give a const to "option_code" in an object.

const option_code = "option1211";
const product_code = "3344";
const size_code = "44"

var product_data = {
    option_code: size_code,
    quantity: "1",
    product_id: product_code,
}

I want a result like this (option code needs to change) =>

{ option1211:'44', quantity:'1', product_id:'3344' }

Is this possible ?

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

0

You're close, you just need to add square brackets around the key name:

var product_data = {
    [option_code]: size_code,
    quantity: "1",
    product_id: product_code,
}

Otherwise, you can do the following:

var product_data = {
    quantity: "1",
    product_id: product_code,
}

product_data[option_code] = size_code;
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!