Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #38196
| Path | csiph.com!weretis.net!feeder7.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.247.232.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Search array of objects |
| Date | Mon, 24 Feb 2020 12:23:05 +0100 |
| Organization | PointedEars Software (PES) |
| Lines | 109 |
| Message-ID | <2373271.grBdAzqF5M@PointedEars.de> (permalink) |
| References | <i_adnaLrBajpv87DnZ2dnUU7-KfNnZ2d@westnet.com.au> <o8adnRKpOaYkoM7DnZ2dnUU7-XnNnZ2d@westnet.com.au> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| Injection-Info | gwaiyur.mb-net.net; posting-host="247.232.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.232.247"; logging-data="11847"; mail-complaints-to="abuse@open-news-network.org" |
| User-Agent | KNode/4.14.10 |
| Cancel-Lock | sha1:PDjmPLWPsdVXoka+rfBvTirEJwo= |
| X-Face | %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| X-User-ID | U2FsdGVkX1+KAt+sF7HYxDjMwYBMTXVc5azRB/LTxPWJB3U4Sa0kPQ== |
| 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== |
| Xref | csiph.com comp.lang.javascript:38196 |
Show key headers only | 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
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