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

125
Views
Javascript Object: Setting a value to be equal to a previous Key

In javascript, is there anyway to set a value equal to a previous key that was declared during initialization? for example,

const object = {
  a: "test",
  b: a
    }
 
 console.log(object)

or

const object = {
  a: "test",
  b: object.a
    }
 
 console.log(object)

How can I achieve this result in the console?

    { a: 'test', b: 'test' }

I understand that this is redundant, so let me explain what I'm trying to achieve.

I want to be able to set a url in one value, and then add to it using a previous value. The reason for this is to clean up the code and set formulas into one object.

Example

const object = {
  base_url: "www.google.com",
  images_url: `${base_url}/images`,
  maps_url: `${base_url}/maps`
    }
 
 console.log(object)

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

0

The closest thing I can think of in Javascript is to use a getter.

const object = {
  a: "test",
  get b(){
   return this.a+"something else";
  }
    }
 
console.log(object)

about 4 years ago · Juan Pablo Isaza Report

0

Getter/Setters will save your day

const object = {
  a: "test",
  get b(){return this.a}
 }
 
 console.log(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!