Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39420

Re: Referencing an object prop with a string

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Referencing an object prop with a string
Date 2021-02-02 04:06 +0100
Organization PointedEars Software (PES)
Message-ID <806120824.0ifERbkFSE@PointedEars.de> (permalink)
References <Vt-dnSg5Kd2Iuov9nZ2dnUU7-Q3NnZ2d@westnet.com.au> <5793bf43-6506-4351-bdca-3d59d740c5e5n@googlegroups.com>

Show all headers | View raw


Michael Haufe (TNO) wrote:

> let walk = (o, path)=>path.split('.').reduce((o,k)=>o && o[k], o);

This does not work if the property name contains `.'.  The approach to use 
Array.prototype.reduce() is an elegant one, though, and can be applied to 
supporting a path array (too), which solves that problem:

  let walk = (o, path) => path.reduce((o, k) => o && o[k], o);
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Referencing an object prop with a string Andrew Poulos <ap_prog@hotmail.com> - 2021-01-31 14:42 +1100
  Re: Referencing an object prop with a string "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-01-30 19:52 -0800
    Re: Referencing an object prop with a string "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-01-30 19:54 -0800
    Re: Referencing an object prop with a string Andrew Poulos <ap_prog@hotmail.com> - 2021-01-31 20:29 +1100
      Re: Referencing an object prop with a string H e <he12457@hotmail.com> - 2021-01-31 02:17 -0800
    Re: Referencing an object prop with a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-02-02 04:06 +0100
      Re: Referencing an object prop with a string "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-02-02 13:01 -0800
        Re: Referencing an object prop with a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-02-02 23:50 +0100
          Re: Referencing an object prop with a string "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-02-02 19:19 -0800
            Re: Referencing an object prop with a string "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-02-02 19:26 -0800
            Re: Referencing an object prop with a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-02-03 19:42 +0100

csiph-web