1

I have an object.

var o = {a : {b : 1}};

In order to access the value of o.a.b via string 'a.b', I will normally think about this.

var ab = 'a.b'.split('.');
console.log(o[ab[0]][ab[1]]);

Is there a more elegant way of doing the same stuff?

user3925697
  • 609
  • 2
  • 8
  • 17
  • Option 1: `eval` (if you are sure data is safe). Option 2: helper function like in [this](http://stackoverflow.com/questions/27978593/access-nested-objects-in-js/27978873#27978873) answer. – dfsq Mar 04 '15 at 07:49
  • Dear Mr. Alnitak, none of the methods in your link is more elegant than mine. I mean my question is not duplicated. – user3925697 Mar 04 '15 at 07:52
  • @user3925697 I beg to differ. Your method is inelegant because it is not general purpose. It does not extend to more (or less) deeply nested objects without changing the way you access those objects. – Alnitak Mar 04 '15 at 07:56
  • @Alnitak You're right in general, not my case. I just have a very simple object. What I really mean is similar to dfsq's comment. – user3925697 Mar 04 '15 at 08:04
  • you mean in the answer he linked to? That's functionally similar to in the linked question. – Alnitak Mar 04 '15 at 09:40

0 Answers0