Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!news.albasani.net!news.mb-net.net!open-news-network.org!.POSTED.63.227.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: get set value in array of objects Date: Wed, 13 Jun 2018 05:25:49 +0200 Organization: PointedEars Software (PES) Lines: 28 Message-ID: <1754859.yDWCITohsU@PointedEars.de> References: <871sdchv1w.fsf@bsb.me.uk> <1609990.zEmimHbLjD@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="63.227.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch:178.197.227.63"; logging-data="21261"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 Cancel-Lock: sha1:Fsh3Rwcm0EHqjXC4U8yIby9LPDI= X-User-ID: U2FsdGVkX1/Ebz3aeqkVzgDq8M9s2r//m43JDwQizYEJXjznWnYPEA== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Ben Bacarisse wrote: >> Essentially you build an array where idx[3] is a reference to the object >> with id === 3. > > A possibility: > > /* Use “= []” for an Array instance instead */ > let id2objMap = Object.create(null); > > objArray.forEach((el) => {this[el.id] = el}, id2objMap); objArray.forEach(function (el) { this[el.id] = el; }, id2objMap); or (probably less efficient) objArray.forEach((el) => {id2objMap[el.id] = el}); > /* true */ > id2objMap[3].id === 3 See the clarification further below for details. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.