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

205
Views
How to create a new nested property in an object?

If I create an object by doing let obj = {}, I can create a new link property by doing obj.link = 'test' and it works.

What if I want instead to create a nested property?

The code below gives an undefined error:

let obj = {}
obj.link.link = 'test'
console.log(obj.link.link)

What's the right way to do this?

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

0

You're trying to access link property inside link, which is not there by the time. So you can do below things.

obj = {}
obj.link = {};
obj.link.link = 'test';
console.log(obj.link.link);
console.log(obj);

Or

 obj = {}
 obj.link = {link: 'test'}
 console.log(obj.link.link);
 console.log(obj);

about 4 years ago · Juan Pablo Isaza Report

0

You need to have an object to address a property inside of an object.

Toassign use a default object and assign with the value to the last property.

let obj = {};

(obj.link ??= {}).link = 'test';

console.log(obj.link.link);
console.log(obj);

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!