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

How to perform deep optional assignment in Javascript?

Say I have an object named foo, and I would like to perform the following operation:

foo.bar.baz.qwer.asdf = 5;

The issue is that bar may not exist, but if it does exist, then baz inside of it may not exist, etc.

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

I thought I would be able to do something like this:

foo?.bar?.baz?.qwer?.asdf = 5, but alas, that sort of chaining only seems to work with retrieving values, not setting values.

The desired behavior is that if something like baz already exists, then that object is just accessed (and its existing members are maintained), but if baz does not exist, then that property is created on the fly.

That is, let’s say only foo.bar existed. Then after the above assignment operation, the resulting object would be:

foo {
  bar: {
    baz: {
      qwer: {
        asdf: 5
      }
    }
  }
}

So my question is: can this be done with existing language syntax constructs, or must a helper function be created? If so, does this sort of function have a name that I could search for and reference?

>Solution :

I have written this:

function setProperty(obj, property, value) {
    const properties = property.split('.');
    const lastProperty = properties.pop();
    for (let prop of properties) {
        if (!obj[prop]) {
            obj[prop] = {};
        }
        obj = obj[prop];
    }
    obj[lastProperty] = value;
}

const startObj = {'a': {}};

setProperty(startObj, 'a.b.c.d.e', 'e is the best letter');
console.log(startObj);

However, it will have unexpected behaviors if one of the intermediate properties exists but isn’t an object. Eg test the above function with the starting object {a:{b:2}}

Also, please note that what obj means changes as the function processes. At first, it means the base object, but it goes on to mean each nested object during the loop.

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