Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: get set value in array of objects Date: Tue, 12 Jun 2018 16:40:59 +0100 Organization: A noiseless patient Spider Lines: 68 Message-ID: <871sdchv1w.fsf@bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="c58388d4db322ae35542968121a34316"; logging-data="26530"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+00UgfQ9+3bl1Z/INhtgbEgs6mw1Fg7RU=" Cancel-Lock: sha1:W86HB/C2aL7F7Wa/e2mZgfrjXgM= sha1:M9uYztdV8J5Vrd6wyg+GSGN6lgY= X-BSB-Auth: 1.c9e4e06ef8ecc5d8ea06.20180612164059BST.871sdchv1w.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:35218 Andrew Poulos writes: > Say I have an array like this > > var objArray = [ > { id:0, name:'Jonah', otherProp:321 }, > { id:1, name:'Andrew', otherProp:648 }, > { id:2, name:'Another', otherProp:850 }, > { id:3, name:'Quite', otherProp:146 }, > { id:4, name:'Mary', otherProp:984 } > ]; > > except that the objects have many more properties and the array has > many more elements. > > > To get, say the 'name' value for the object with an id = 3 I'm using: > > var val = objArray.find(function(obj) { > return (obj.id === 3 ? obj.name : false); > }); That's a bit odd. Maybe you meant var val = objArray.find(function(obj) { return obj.id === 3; }).name; The return from the function passed to Array.prototype.find is simly used as a truth value. Returning obj.name might be deliberate (you'd pass over elements with a matching is but a name that is "falsey") but the wording suggests you think you might be getting the name returned from find. > 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 so you could base both of these operations on a function that returns (a reference to) the object: function oid(id) { return objArray.find(function(obj) { return obj.id === id; }); } (though you may want to parameterise it with the array as well). With this, oid(3).name is the name and oid(3).name = "Fred" sets the name. If you do a great many look-ups, it might pay to make an "index" object. If the IDs are always small integers, this could be an array, but in general it might need to be an object with non-numeric properties. Essentially you build an array where idx[3] is a reference to the object with id === 3. If the original array changes the id to object mapping, this index array needs to be temporary, or you must find a way to keep it synchronised with the original. (This post got delayed. I finished it anyway, but I bet you've had all the answers you need already.) -- Ben.