Is there a library to support autovivification on Javascript objects?

非 Y 不嫁゛ 提交于 2019-12-01 04:21:10

You can't do it exactly with the syntax you want. But as usual, in JS you can write your own function:

function set (obj,keys,val) {
    for (var i=0;i<keys.length;i++) {
        var k = keys[i];
        if (typeof obj[k] == 'undefined') {
            obj[k] = {};
        }
        obj = obj[k];
    }
    obj = val;
}

so now you can do this:

// as per you example:
set(foo,['bar','baz'],5);

without worrying if bar or baz are defined. If you don't like the [..] in the function call you can always iterate over the arguments object.

Purely natively, I don't think so. undefined isn't extensible or changeable and that's about the only way I could imagine doing it without passing it through a function.

I had a desire to do this, so I wrote a package to handle it.

% npm install autovivify
% node
> Av = require('autovivify')
> foo = new Av()
{}
> foo.bar.baz = 5
5
> foo
{ bar: { baz: 5 } }
>

It'll even do arrays with numeric subscripts:

> foo = new Av()
> foo.bar.baz[0] = 'hum'
> foo
{ bar: { baz: [ 'hum' ] } }

Or you can use an eval-based solution. It's ugly, not recommended.

function av(xpr) {

    var res = "";
    var pos = 0;
    while (true) {

        var pos = xpr.indexOf("[",pos);
        if (pos == -1) break;
        var frag = xpr.substr(0,pos);
        pos++;

        res += "if (typeof(" + frag + ") != 'object') " + frag + " = {};\n";
    } // while

    return res + xpr;
} // av()


function main() {

    var a = {};
    a["keep"] = "yep";
    eval(av('a[1][1]["xx"] = "a11xx";   '));
    eval(av('a[1][2]["xx"] = "a12xx";   '));

    console.log(a);
} // main()

@slebetman's code doesn't seem to work. The last key should not be assigned an empty object, but rather the val. This code worked:

function autoviv(obj,keys,val) {
  for (var i=0; i < keys.length; i++) {
    var k = keys[i];
    if (typeof obj[k] === 'undefined') {
      if(i === keys.length-1) {
        obj[k] = val;
        return;
      }
      obj[k] = {};
    }
    obj = obj[k];
  }
}

foo = {}
autoviv(foo,['bar','baz'],5);
console.log(foo.bar.baz);
5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!