0

I want to assign a value to a child of an object in javascript like:

let myobj.level1.level2.level3.x = 100;

The problem is x, level1, level2, level3 may or may not exist.

Do I have to check and create each one of the levels? Is there an easy way to do this?

Feng Jiang
  • 1,776
  • 19
  • 25
  • Possible duplicate of [Is there a library to support autovivification on Javascript objects?](https://stackoverflow.com/questions/13498330/is-there-a-library-to-support-autovivification-on-javascript-objects) – jcaron Oct 04 '17 at 18:04
  • This is called auto-vivification, and there's no native construct for this in Javascript (contrary to perl for instance). See https://stackoverflow.com/questions/13498330/is-there-a-library-to-support-autovivification-on-javascript-objects for a workaround. – jcaron Oct 04 '17 at 18:05
  • Thanks. There is no way I would know that term. – Feng Jiang Oct 04 '17 at 18:09

0 Answers0