Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #35218

Re: get set value in array of objects

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: get set value in array of objects
Date 2018-06-12 16:40 +0100
Organization A noiseless patient Spider
Message-ID <871sdchv1w.fsf@bsb.me.uk> (permalink)
References <m7idnSrECaxLhYLGnZ2dnUU7-fvNnZ2d@westnet.com.au>

Show all headers | 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


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