0

Having

var path = "a.b.c";
var foo = {a: {b: {c: "bar"}}};

How to get the "bar" from foo and path?

Surely the foo['a']['b']['c'] will work but how to get it nicely?

serge
  • 13,940
  • 35
  • 121
  • 205
  • seems the `'a.b.c'.split('.').reduce((p,c)=>p&&p[c]||null, foo)` solves it, thanks @Dekel & @Adriano Spadoni – serge Apr 11 '20 at 00:24

1 Answers1

0

Manually handling it would be great trouble. Try this. https://www.npmjs.com/package/object-path

  • thanks, but , no, I need pure and simple javascript solution – serge Apr 11 '20 at 00:16
  • Are you looking for a dynamic solution with a variable length of `path` like `path = "a.b.c.d.e..."` or just a static solution for this three levelled structure ? – Dipansh Khandelwal Apr 11 '20 at 00:19
  • it seems the "great troube" is this one: `'a.b.c'.split('.').reduce((p,c)=>p&&p[c]||null, foo)` – serge Apr 11 '20 at 00:23