Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #35218
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
| 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> (permalink) |
| References | <m7idnSrECaxLhYLGnZ2dnUU7-fvNnZ2d@westnet.com.au> |
| 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 |
Show key headers only | View raw
Andrew Poulos <ap_prog@hotmail.com> 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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-12 10:50 +1000
Re: get set value in array of objects "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-06-12 16:40 +0200
Re: get set value in array of objects "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-06-12 17:12 +0200
Re: get set value in array of objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2018-06-12 17:36 +0200
using Map Was: Re: get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-13 13:56 +1000
Re: using Map Was: Re: get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-13 14:43 +1000
Re: using Map Was: Re: get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-13 17:17 +1000
Re: using Map Was: Re: get set value in array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 23:32 +0200
Re: using Map Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 18:47 +0200
Re: using Map Andrew Poulos <ap_prog@hotmail.com> - 2018-06-14 21:52 +1000
Re: using Map Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-14 14:31 +0200
Re: using Map Andrew Poulos <ap_prog@hotmail.com> - 2018-06-14 23:52 +1000
Re: using Map Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-14 17:13 +0200
Re: using Map "Mark-" <nospam@mapson.xyz> - 2018-06-14 16:25 +0000
Re: using Map "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-06-14 16:11 +0200
Re: get set value in array of objects Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-06-12 16:40 +0100
Re: get set value in array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 05:18 +0200
Re: get set value in array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 05:25 +0200
Re: get set value in array of objects Julio Di Egidio <julio@diegidio.name> - 2018-06-14 07:11 -0700
csiph-web