-2

In JavaScript object, we can

var myobj = {
    x: 0,
    y: {
        n: 6
    }
};
myobj['x'] = 6; // myobj.x will be 6

Can I do something like this with a function?—

myobj['y.n'] = 6;

I don't want use myobj.y.n.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Wael Boutglay
  • 1,163
  • 1
  • 15
  • 21

1 Answers1

2

No, there is no built-in way to resolve this kind of object path. However, there are quite a few small libraries available for this such as goodwin.

Or you could implement it on your own. It's not really hard. Simply split the path string at . and then walk through the objects.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636