Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!news.roellig-ltd.de!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:18:51 +0200 Organization: PointedEars Software (PES) Lines: 120 Message-ID: <1609990.zEmimHbLjD@PointedEars.de> References: <871sdchv1w.fsf@bsb.me.uk> 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="19890"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-User-ID: U2FsdGVkX1/Pa5K5X/XOcltoEfPw3s/WxsmfwA8yTCE8wyuIwAHJuw== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Andrew Poulos writes: >> And to set the 'name' value for the object with an id = 3 I'm using: >> >> var ind = objArray.findIndex(function(obj) { >> return obj.id === 3; >> }); >> >> objArray[ind]['name'] = "Fred"; >> >> It works but I'm wondering if someone could show a "better" way. > > You don't need the index since objects are shared by reference in > ECMAScript Only in the broadest possible sense. There is no concept of “share by reference” in programming. The wording probably stems from the idea that objects would be “call-by-reference” and other values would be “call-by- value”, which is a common *misconception* about ECMAScript (implementations). Instead, *all* values are “call-by-value”, but objects’ properties can only be accessed through references to objects; so those references are the passed values then. (This is not exclusive to ECMAScript.) Also, this logic is flawed. One does not need the “id” as index *here* in this *particular* example, because the ”id” property value happens to be the index of the array element that is the reference to the respective Object instance *here*. > If you do a great many look-ups, it might pay to make an "index" object. I find that a strange way to name things. An “index object” would be an object that holds information about one index instead, would it not? > If the IDs are always small integers, this could be an array, Whereas "small" is from 0 to 2³²−2 (= 4294967294), and “array” is “_Array instance_”: (1997) through (2017). For Array-like objects with larger indexes, e.g. jsx.array.BigArray can be used: > 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); /* true */ id2objMap[3].id === 3 Corresponding to Array.from(), there should be an Object.from(objArray, …) for this: if (!(Object.from instanceof Function)) { Object.from = ((iterable, mapping, thisArg) => { var result = thisArg || Object.create(null); if (typeof mapping == "undefined") { throw new TypeError("Mapping not specified"); } let mapfn = mapping; if (!(mapping instanceof Function)) { mapfn = function (el) { this[el[mapping]] = el; }; } Array.from(iterable).forEach(mapfn, result); return result; }); } let id2objMap = Object.from(objArray, "id"); BTW, mapfn = ((el) => { this[el[mapping]] = el; }); instead does _not_ work. Because despite the callback being called with “result” as the “this” value, “this” in the arrow function will stubbornly refer to the global object in non-strict mode, and be “undefined” in strict mode. This is the result of an IMHO bad design decision/oversight that unnecessarily severely limits the use-cases of the useful arrow function shorthand. By contrast, the more backwards-compatible, but longer, function expression works in both modes there. > (This post got delayed. I finished it anyway, but I bet you've had all > the answers you need already.) It might have been better then if you had just not posted. Consider that you do not have to save the world alone, especially if you are unsuited to do so. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.