Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object "Nesting"?

I want to use an object in nodejs to store url paths, and reuse a previous entry for example:

let URLS = {
  root: "www.example.com",
  paths: {
    user: root + "/user",
    login: root + "/login"
  } 
}

is there a way to achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot access URLS.root before the object is created, but you can define a separated variable root and use that in multiple places in the URLS object:

const root = "www.example.com";

const URLS = {
  root,
  paths: {
    user: root + "/user",
    login: root + "/login"
  } 
}

console.log(URLS)

In the example above, the root property is defined using a shorthand property names instead of writing the property name and the property value separately (e.g. { root: root }).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading