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


Groups > comp.lang.javascript > #38196

Re: Search array of objects

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Search array of objects
Date 2020-02-24 12:23 +0100
Organization PointedEars Software (PES)
Message-ID <2373271.grBdAzqF5M@PointedEars.de> (permalink)
References <i_adnaLrBajpv87DnZ2dnUU7-KfNnZ2d@westnet.com.au> <o8adnRKpOaYkoM7DnZ2dnUU7-XnNnZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> On 24/02/2020 12:10 pm, Andrew Poulos wrote:
>> I have an array of 50+ arrays each containing 300+ objects as elements.

Technically it is not actually containing the objects, but just the 
references to them.  You can use that to your advantage (see below).

>> Each individual object looks like
>> 
>> {
>>  type: "text",
>>  id: "text01",
>>  content: "Turn to left",
>>  disabled: false,
>>  bg: "small.jpg"
>> }
>> 
>> So the entire array looks like
>> 
>> [
>>  [obj1, obj2, obj3...],
>>  [obj1, obj2, obj3...],
>>  [obj1, obj2, obj3...],
>>  ...
>> ]
>> 
>> That is, every sub-array has the same number of objects: 'content',
>> 'disabled' and 'bg' may differ from sub-array to sub-array.
>> 
>> I need to change the value of 'bg' for the "same" five objects across
>> all the sub-arrays.
>> 
>> Currently I'm looping through the top level array and then each object
>> in a sub-array looking for the objects in question. Is there a more
>> efficient way to do this without looping?
> 
> I tried this (nesting forEach):
> 
> slides.forEach(s => {
>    s.forEach(item => {

You should rename “s” to “slide”.

>      if (item.type == 'text' && item.hasOwnProperty('content')) {

If you can make sure beforehand that the prototype chain of “item” is empty 
(and you should), then you can use the “in” operator which might be more 
efficient that multiple .hasOwnProperty() calls.

A simple way to achieve that is

  obj = Object.create(null, Object.getOwnPropertyDescriptors(obj));

You can still use methods of Object.prototype on this object by calling them 
directly on it.

JSX:object.js has (deprecated, master) jsx.object.getDataObject() and 
(trunk) jsx.object.createDataObject() for this purpose, but it does not
use the new approach yet.

>        item.disabled = !chk; // a value set earlier

You are not doing here what you described above.

>      }
>    };
> });
> 
> and it seems to work. Is this a good approach?

Probably yes; I would not do it much differently.

If you operate on the same data structure or data several times, you can use 
Array.prototype.filter() to retrieve an array of references to only those 
objects that are of interest, and subsequently only use that array:

  const contentObjs = [];

  slides.forEach(slide => {
    contentObjs.push.apply(
      contentObjs,
      slide.filter(item => 'content' in item)
    );
  });

PoC (suitable to experiment with in the script console):

  var contentObjs = [];
  [[{content: 42}, {no_content: 23}]].forEach(slide => {
    contentObjs.push.apply(
      contentObjs,
      slide.filter(item => 'content' in item)
    );
  });

  /* “[{…}]” */
  console.log(contentObjs);

However, you appear to be doing something like this a lot recently; you 
might want to look into Microsoft LINQ, which is a specification for an API 
and a library with implementations in various languages (including 
ECMAScript implementations) that is specifically designed to handle arrays 
of objects efficiently.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Search array of objects Andrew Poulos <ap_prog@hotmail.com> - 2020-02-24 12:10 +1100
  Re: Search array of objects Andrew Poulos <ap_prog@hotmail.com> - 2020-02-24 14:06 +1100
    Re: Search array of objects luserdroog <luser.droog@gmail.com> - 2020-02-23 21:33 -0800
    Re: Search array of objects Julio Di Egidio <julio@diegidio.name> - 2020-02-24 02:02 -0800
    Re: Search array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-02-24 12:23 +0100
      Re: Search array of objects John G Harris <niam@jghnorth.org.uk.invalid> - 2020-02-24 18:31 +0000
        Re: Search array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-02-25 09:24 +0100
          Re: Search array of objects John G Harris <niam@jghnorth.org.uk.invalid> - 2020-02-25 11:40 +0000
            Re: Search array of objects Julio Di Egidio <julio@diegidio.name> - 2020-02-25 03:57 -0800
            Re: Search array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-02-25 19:02 +0100
              Re: Search array of objects John G Harris <niam@jghnorth.org.uk.invalid> - 2020-02-25 19:18 +0000
                Re: Search array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-02-25 21:51 +0100
                Re: Search array of objects John G Harris <niam@jghnorth.org.uk.invalid> - 2020-02-26 16:09 +0000
            Re: Search array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-02-25 19:09 +0100
              Re: Search array of objects "Mark-" <nospam@mapson.xyz> - 2020-02-25 19:18 +0000
                Re: Search array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-02-25 21:58 +0100
                Re: Search array of objects John G Harris <niam@jghnorth.org.uk.invalid> - 2020-02-26 16:09 +0000
              Re: Search array of objects John G Harris <niam@jghnorth.org.uk.invalid> - 2020-02-25 19:47 +0000

csiph-web