Path: csiph.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!news.dns-netz.com!news.freedyn.net!news.mb-net.net!open-news-network.org!.POSTED.50.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Referencing an object prop with a string Date: Tue, 02 Feb 2021 04:06:57 +0100 Organization: PointedEars Software (PES) Lines: 14 Message-ID: <806120824.0ifERbkFSE@PointedEars.de> References: <5793bf43-6506-4351-bdca-3d59d740c5e5n@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit Injection-Info: gwaiyur.mb-net.net; posting-host="50.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.236.50"; logging-data="21118"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Lock: sha1:oGZLfIdmCECR7GCF8vAhVUHUp/k= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. 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: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.