Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33577 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2017-07-16 19:00 +1000 |
| Last post | 2017-07-18 13:02 -0300 |
| Articles | 11 — 7 participants |
Back to article view | Back to comp.lang.javascript
Find object in array Andrew Poulos <ap_prog@hotmail.com> - 2017-07-16 19:00 +1000
Re: Find object in array Martin Honnen <martin.honnen@gmx.de> - 2017-07-16 11:11 +0200
Re: Find object in array JR <jrfaqcom@gmail.com> - 2017-07-17 12:47 -0300
Re: Find object in array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-07-17 20:06 +0200
Re: Find object in array Andrew Poulos <ap_prog@hotmail.com> - 2017-07-16 19:14 +1000
Re: Find object in array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-07-17 01:30 +0200
Re: Find object in array Dr J R Stockton <J.R.Stockton@physics.org> - 2017-07-16 15:13 -0700
Re: Find object in array Andrew Poulos <ap_prog@hotmail.com> - 2017-07-17 14:49 +1000
Re: Find object in array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-17 13:53 +0200
Re: Find object in array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-07-17 14:29 +0100
Re: Find object in array JR <jrfaqcom@gmail.com> - 2017-07-18 13:02 -0300
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2017-07-16 19:00 +1000 |
| Subject | Find object in array |
| Message-ID | <Ca-dndZsZ7OrtvbEnZ2dnUU7-e3NnZ2d@westnet.com.au> |
The server returns a string which I parse. It becomes an array of
"simple" objects eg.
{
"id":1,
"colour": "#fff",
"width": 320,
"height":240
}
To find an object with a specific "id" I use a for loop on the array.
This is ok but now the arrays I'm getting contain 10s of objects. What
better way can I use to find a specific object?
Andrew Poulos
[toc] | [next] | [standalone]
| From | Martin Honnen <martin.honnen@gmx.de> |
|---|---|
| Date | 2017-07-16 11:11 +0200 |
| Message-ID | <okfajg$fug$1@news.albasani.net> |
| In reply to | #33577 |
On 16.07.2017 11:00, Andrew Poulos wrote:
> The server returns a string which I parse. It becomes an array of
> "simple" objects eg.
> {
> "id":1,
> "colour": "#fff",
> "width": 320,
> "height":240
> }
>
> To find an object with a specific "id" I use a for loop on the array.
> This is ok but now the arrays I'm getting contain 10s of objects. What
> better way can I use to find a specific object?
If you have an array "a" and want to find items meeting a specific
condition then you can use
a.filter(o => o.id == 1)
to get an array of those objects meeting the filter condition.
var a = [{id: 1, data: 'foo'}, {id: 2, data: 'bar'}, {id:1, data: 'baz'}];
a.filter(o => o.id == 1)
[toc] | [prev] | [next] | [standalone]
| From | JR <jrfaqcom@gmail.com> |
|---|---|
| Date | 2017-07-17 12:47 -0300 |
| Message-ID | <okim6r$1849$1@gioia.aioe.org> |
| In reply to | #33578 |
Martin Honnen wrote:
> On 16.07.2017 11:00, Andrew Poulos wrote:
>> The server returns a string which I parse. It becomes an array of
>> "simple" objects eg.
>> {
>> "id":1,
>> "colour": "#fff",
>> "width": 320,
>> "height":240
>> }
>>
>> To find an object with a specific "id" I use a for loop on the array.
>> This is ok but now the arrays I'm getting contain 10s of objects. What
>> better way can I use to find a specific object?
>
> If you have an array "a" and want to find items meeting a specific
> condition then you can use
> a.filter(o => o.id == 1)
> to get an array of those objects meeting the filter condition.
>
> var a = [{id: 1, data: 'foo'}, {id: 2, data: 'bar'}, {id:1, data: 'baz'}];
> a.filter(o => o.id == 1)
Great!
Another possibility is the find() method [1]:
a.find(el => el.id === 1);
[1]
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find>
--
Joao Rodrigues
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-07-17 20:06 +0200 |
| Message-ID | <3355443.kQq0lBPeGt@PointedEars.de> |
| In reply to | #33585 |
JR wrote:
> Martin Honnen wrote:
>> If you have an array "a" and want to find items meeting a specific
>> condition then you can use
>> a.filter(o => o.id == 1)
>> to get an array of those objects meeting the filter condition.
>>
>> var a = [{id: 1, data: 'foo'}, {id: 2, data: 'bar'}, {id:1, data:
>> 'baz'}]; a.filter(o => o.id == 1)
>
> Great!
>
> Another possibility is the find() method [1]:
>
> a.find(el => el.id === 1);
>
> [1]
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find>
In contrast to .filter(), .find() does not return a reference to a new Array
instance of matching elements (here: object references), but only the first
matching element.
--
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.
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2017-07-16 19:14 +1000 |
| Message-ID | <TIednVVGO7Hks_bEnZ2dnUU7-X_NnZ2d@westnet.com.au> |
| In reply to | #33577 |
On 16/07/2017 7:00 PM, Andrew Poulos wrote:
> The server returns a string which I parse. It becomes an array of
> "simple" objects eg.
> {
> "id":1,
> "colour": "#fff",
> "width": 320,
> "height":240
> }
>
> To find an object with a specific "id" I use a for loop on the array.
> This is ok but now the arrays I'm getting contain 10s of objects. What
> better way can I use to find a specific object?
Doh, I just realised that I can use filter.
On a related question would it be "faster" to use a for loop, instead of
multiple filters, if I needed to find a number of objects?
Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-07-17 01:30 +0200 |
| Message-ID | <1812688.oMNUckLgyt@PointedEars.de> |
| In reply to | #33579 |
Andrew Poulos wrote:
> On 16/07/2017 7:00 PM, Andrew Poulos wrote:
>> The server returns a string which I parse. It becomes an array of
>> "simple" objects eg.
>> {
>> "id":1,
>> "colour": "#fff",
>> "width": 320,
>> "height":240
>> }
>>
>> To find an object with a specific "id" I use a for loop on the array.
>> This is ok but now the arrays I'm getting contain 10s of objects. What
>> better way can I use to find a specific object?
>
> Doh, I just realised that I can use filter.
>
> On a related question would it be "faster" to use a for loop, instead of
> multiple filters, if I needed to find a number of objects?
You would not need multiple .filter() calls to find a number of objects.
As to whether .filter() would be faster than a “for” loop, you should test
it. A “for” loop should be faster, because it does not incur performance
costs for calling the function and resolving identifiers along a longer
scope chain.
BTW, this wheel has been invented before, and it has been called “Language-
Integrated Query (LINQ)”. Also, my JSX:array.js:jsx.array.createFilter()
helps you generate a filter callback for filtering objects.
--
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.
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| Date | 2017-07-16 15:13 -0700 |
| Message-ID | <08e1f336-533b-44be-9e79-ca6f39604915@googlegroups.com> |
| In reply to | #33577 |
On Sunday, July 16, 2017 at 10:00:52 AM UTC+1, Andrew Poulos wrote:
> The server returns a string which I parse. It becomes an array of
> "simple" objects eg.
> {
> "id":1,
> "colour": "#fff",
> "width": 320,
> "height":240
> }
>
> To find an object with a specific "id" I use a for loop on the array.
> This is ok but now the arrays I'm getting contain 10s of objects. What
> better way can I use to find a specific object?
It is not an array of simple objects; it is an object with
properties. If it was an array, it would have the special
'length' property.
Demo :
var Q = {
"id":1,
"colour": "#fff",
"width": 320,
"height":240
}
Q["width"] // = 320
--
(c) John Stockton, near London, UK. Using Google, no spell-check. |
Mail: J.R.""""""""@physics.org |
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2017-07-17 14:49 +1000 |
| Message-ID | <dZudnZVZxoFY3PHEnZ2dnUU7-ffNnZ2d@westnet.com.au> |
| In reply to | #33580 |
On 17/07/2017 8:13 AM, Dr J R Stockton wrote:
> On Sunday, July 16, 2017 at 10:00:52 AM UTC+1, Andrew Poulos wrote:
>> The server returns a string which I parse. It becomes an array of
>> "simple" objects eg.
>> {
>> "id":1,
>> "colour": "#fff",
>> "width": 320,
>> "height":240
>> }
>>
>> To find an object with a specific "id" I use a for loop on the array.
>> This is ok but now the arrays I'm getting contain 10s of objects. What
>> better way can I use to find a specific object?
>
> It is not an array of simple objects; it is an object with
> properties. If it was an array, it would have the special
> 'length' property.
>
> Demo :
>
> var Q = {
> "id":1,
> "colour": "#fff",
> "width": 320,
> "height":240
> }
>
> Q["width"] // = 320
My bad for being ambiguous. The example was of a simple object (that is
an element of the array) and not of the array itself.
Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-07-17 13:53 +0200 |
| Message-ID | <XnsA7B58D4EB2797eejj99@194.109.6.166> |
| In reply to | #33582 |
Andrew Poulos <ap_prog@hotmail.com> wrote on 17 Jul 2017 in
comp.lang.javascript:
> On 17/07/2017 8:13 AM, Dr J R Stockton wrote:
>> On Sunday, July 16, 2017 at 10:00:52 AM UTC+1, Andrew Poulos wrote:
>>> The server returns a string which I parse. It becomes an array of
>>> "simple" objects eg.
>>> {
>>> "id":1,
>>> "colour": "#fff",
>>> "width": 320,
>>> "height":240
>>> }
>>>
>>> To find an object with a specific "id" I use a for loop on the array.
>>> This is ok but now the arrays I'm getting contain 10s of objects. What
>>> better way can I use to find a specific object?
>>
>> It is not an array of simple objects; it is an object with
>> properties. If it was an array, it would have the special
>> 'length' property.
>>
>> Demo :
>>
>> var Q = {
>> "id":1,
>> "colour": "#fff",
>> "width": 320,
>> "height":240
>> }
>>
>> Q["width"] // = 320
>
> My bad for being ambiguous. The example was of a simple object (that is
> an element of the array) and not of the array itself.
No, that is not an array.
An array has non-negative integer numbered members and a 'length'-property,
and can be given other properties that do not affect [and be affected by]
the length-value.
'use strict';
let a = ['q','w','e',,,'five'];
a[17] = 'top';
a.once = '1x';
a['twice'] = '2x';
alert(a[1]); // w
alert(a[5]); // five
alert(a[17]); // top
alert(a['length']); // 17
alert(a['once']); // 1x
alert(a.twice); // 2x
a.length = 999;
alert(a['length']); // 999
alert(a[2]); // e
a.length = 2;
alert(a[2]); // undefined
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-07-17 14:29 +0100 |
| Message-ID | <87bmoj9ope.fsf@bsb.me.uk> |
| In reply to | #33583 |
"Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: > Andrew Poulos <ap_prog@hotmail.com> wrote on 17 Jul 2017 in > comp.lang.javascript: <snip> >> My bad for being ambiguous. The example was of a simple object (that is >> an element of the array) and not of the array itself. Your post was clear so I don't think you need to apologise, but I suppose it might smooth things over. > No, that is not an array. Indeed, but the OP never said it was! The original post was quite clear (at least to me) but now replies to the unnecessary corrections are being corrected! It's got so meta it has nothing to do with the original question. <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | JR <jrfaqcom@gmail.com> |
|---|---|
| Date | 2017-07-18 13:02 -0300 |
| Message-ID | <oklbf4$1dkr$1@gioia.aioe.org> |
| In reply to | #33577 |
On 16-07-2017 06:00, Andrew Poulos wrote:
> The server returns a string which I parse. It becomes an array of
> "simple" objects eg.
> {
> "id":1,
> "colour": "#fff",
> "width": 320,
> "height":240
> }
It seems this app is using some form of "Virtual DOM", or just assigning
styles (Properties / Values) to a given HTML element. In any case, it
would be better using standard CSS property names, e.g "color" instead
of "colour", and strings for width / height values such as "320px" or
"240px". That's because it would be easier to assign styles to an element:
E.g.
var el = document.getElementById("1");
el.style.cssText = "color:#fff;width:320px;height:240px;";
Or:
Object.assign(el.style,
{
"color": "#fff",
"width": "320px",
"height": "240px"
}
);
> To find an object with a specific "id" I use a for loop on the array.
> This is ok but now the arrays I'm getting contain 10s of objects. What
> better way can I use to find a specific object?
As Martin Honnen suggested, you might use the Array's filter() method to
get an array of those objects meeting the filter condition.
However if "id" references a unique element, you'd be ok with the find()
method too. Look up these two Array methods in the MDN:
<https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array>
--
Joao Rodrigues
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web