Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #36020 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2019-02-15 19:44 +1100 |
| Last post | 2019-02-26 07:43 -0800 |
| Articles | 19 — 8 participants |
Back to article view | Back to comp.lang.javascript
getting number of prop values from array Andrew Poulos <ap_prog@hotmail.com> - 2019-02-15 19:44 +1100
Re: getting number of prop values from array "R.Wieser" <address@not.available> - 2019-02-15 10:15 +0100
Re: getting number of prop values from array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-02-15 15:08 +0100
Re: getting number of prop values from array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-02-15 15:16 +0100
Re: getting number of prop values from array Andrew Poulos <ap_prog@hotmail.com> - 2019-02-16 14:11 +1100
Re: getting number of prop values from array John G Harris <niam@jghnorth.org.uk.invalid> - 2019-02-16 15:37 +0000
Re: getting number of prop values from array Julio Di Egidio <julio@diegidio.name> - 2019-02-16 10:43 -0800
Re: getting number of prop values from array Andrew Poulos <ap_prog@hotmail.com> - 2019-02-17 12:50 +1100
Re: getting number of prop values from array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-02-17 16:23 +0100
Re: getting number of prop values from array Julio Di Egidio <julio@diegidio.name> - 2019-02-17 15:41 -0800
Re: getting number of prop values from array Julio Di Egidio <julio@diegidio.name> - 2019-02-17 16:08 -0800
Re: getting number of prop values from array Andrew Poulos <ap_prog@hotmail.com> - 2019-02-18 11:32 +1100
Re: getting number of prop values from array Julio Di Egidio <julio@diegidio.name> - 2019-02-17 22:13 -0800
Re: getting number of prop values from array Andrew Poulos <ap_prog@hotmail.com> - 2019-02-18 19:56 +1100
Re: getting number of prop values from array Julio Di Egidio <julio@diegidio.name> - 2019-02-18 01:24 -0800
Re: getting number of prop values from array Andrew Poulos <ap_prog@hotmail.com> - 2019-02-18 20:36 +1100
Re: getting number of prop values from array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2019-02-19 19:15 -0800
Re: getting number of prop values from array Mickey Finn <mfinney@gmail.com> - 2019-02-17 13:42 -0300
Re: getting number of prop values from array dr.j.r.stockton@gmail.com - 2019-02-26 07:43 -0800
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2019-02-15 19:44 +1100 |
| Subject | getting number of prop values from array |
| Message-ID | <FfudnXxbaMVs5vvBnZ2dnUU7-f_NnZ2d@westnet.com.au> |
If I have an object that looks like this
{
"links": [
{
"from": "1ff9fce8",
"to": "2cc8ebd7",
"id": "1a8d67fe"
},
{
"from": "2cc8ebd7",
"to": "8d1d8c2b",
"id": "f4ca15f5"
},
{
"from": "2cc8ebd7",
"to": "4c48567f",
"id": "9384e250"
},
{
"from": "8d1d8c2b",
"to": "9e2e9d3c",
"id": "12c2b1b4"
},
{
"from": "2cc8ebd7",
"to": "324db009",
"id": "8c348628"
},
{
"from": "4c48567f",
"to": "9e2e9d3c",
"id": "2cbe2766"
},
{
"from": "324db009",
"to": "9e2e9d3c",
"id": "e715fe9f"
},
...
]
}
how can I make a list of the number of times a 'from' value that appears
more than once appears?
In the example above "2cc8ebd7" appears as 'from' 3 times. I've got some
code with nested for loops that sort of works but I'm struggling to work
out a more robust way.
Andrew Poulos
[toc] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2019-02-15 10:15 +0100 |
| Message-ID | <q45vv3$8vn$1@gioia.aioe.org> |
| In reply to | #36020 |
Andrew, > how can I make a list of the number of times a 'from' value that appears > more than once appears? My first thought is to create an associative array (directory object ?), and put the values of the "from"s into it as keys, with an associated value of 1. When the key already exists just increment the associated value. After having done that you can iterate that associative array for any value thats not 1. Regards, Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2019-02-15 15:08 +0100 |
| Message-ID | <bea9d5e2-063b-2886-3402-dc21a2b49d43@PointedEars.de> |
| In reply to | #36020 |
Andrew Poulos wrote:
> If I have an object that looks like this
>
> {
> "links": [
> {
> "from": "1ff9fce8",
> "to": "2cc8ebd7",
> "id": "1a8d67fe"
> },
> {
> "from": "2cc8ebd7",
> "to": "8d1d8c2b",
> "id": "f4ca15f5"
> },
> {
> "from": "2cc8ebd7",
> "to": "4c48567f",
> "id": "9384e250"
> },
> // […]
> ]
> }
>
> how can I make a list of the number of times a 'from' value that appears
> more than once appears?
>
> In the example above "2cc8ebd7" appears as 'from' 3 times. I've got some
> code with nested for loops that sort of works but I'm struggling to work
> out a more robust way.
var obj = {
links: [
{
"from": "1ff9fce8",
"to": "2cc8ebd7",
"id": "1a8d67fe"
},
{
"from": "2cc8ebd7",
"to": "8d1d8c2b",
"id": "f4ca15f5"
},
{
"from": "2cc8ebd7",
"to": "4c48567f",
"id": "9384e250"
}
]
};
var stats = Object.create(null);
obj.links.forEach((item) => {
var from = item.from;
stats[from] = ((from in stats) ? stats[from] + 1 : 1);
});
/* {1ff9fce8: 1, 2cc8ebd7: 2} */
console.log(stats);
Replace “var” with “let” or “const” in production-quality code (unless it
must be backwards-compatible to ES < 6 and you are not using a transpiler,
in which case you must replace the other expressions as well).
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2019-02-15 15:16 +0100 |
| Message-ID | <adb7150e-6443-54a2-fe4c-f3549dff7bcb@PointedEars.de> |
| In reply to | #36022 |
Thomas 'PointedEars' Lahn wrote:
> […]
> var stats = Object.create(null);
>
> obj.links.forEach((item) => {
> var from = item.from;
> stats[from] = ((from in stats) ? stats[from] + 1 : 1);
> });
>
> /* {1ff9fce8: 1, 2cc8ebd7: 2} */
> console.log(stats);
/* [{from: "2cc8ebd7", count: 2}] */
console.log(
Object.keys(stats)
.map((from) => ({from: from, count: stats[from]}))
.filter((item) => (item.count > 1))
);
> Replace “var” with “let” or “const” in production-quality code (unless it
> must be backwards-compatible to ES < 6 and you are not using a transpiler,
> in which case you must replace the other expressions as well).
--
PointedEars
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 | 2019-02-16 14:11 +1100 |
| Message-ID | <cNWdnX6tqNzB4vrBnZ2dnUU7-fnNnZ2d@westnet.com.au> |
| In reply to | #36023 |
On 16/02/2019 1:16 am, Thomas 'PointedEars' Lahn wrote:
> Thomas 'PointedEars' Lahn wrote:
>> […]
>> var stats = Object.create(null);
>>
>> obj.links.forEach((item) => {
>> var from = item.from;
>> stats[from] = ((from in stats) ? stats[from] + 1 : 1);
>> });
>>
>> /* {1ff9fce8: 1, 2cc8ebd7: 2} */
>> console.log(stats);
>
> /* [{from: "2cc8ebd7", count: 2}] */
> console.log(
> Object.keys(stats)
> .map((from) => ({from: from, count: stats[from]}))
> .filter((item) => (item.count > 1))
> );
>> Replace “var” with “let” or “const” in production-quality code (unless it
>> must be backwards-compatible to ES < 6 and you are not using a transpiler,
>> in which case you must replace the other expressions as well).
Thanks, it's short, easy enough for me to parse, and gives me exactly
the result I needed.
Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2019-02-16 15:37 +0000 |
| Message-ID | <dfbg6e9a2ttbjhm15vub31k1kgjmdkhsf2@4ax.com> |
| In reply to | #36022 |
On Fri, 15 Feb 2019 15:08:27 +0100, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: <snip> >Replace “var” with “let” or “const” in production-quality code (unless it >must be backwards-compatible to ES < 6 and you are not using a transpiler, >in which case you must replace the other expressions as well). Also, you can't create an object with an empty prototype chain if using ES < 5. John
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-02-16 10:43 -0800 |
| Message-ID | <ba4bae79-cedd-4357-a7c7-377bf3b2dfe3@googlegroups.com> |
| In reply to | #36025 |
On Saturday, 16 February 2019 16:37:10 UTC+1, John G Harris wrote: > On Fri, 15 Feb 2019 15:08:27 +0100, Thomas 'PointedEars' Lahn > <PointedEars@web.de> wrote: > <snip> > >Replace “var” with “let” or “const” in production-quality code (unless it > >must be backwards-compatible to ES < 6 and you are not using a transpiler, > >in which case you must replace the other expressions as well). > > Also, you can't create an object with an empty prototype chain if > using ES < 5. FWIW, I'd give the exact opposite advice: there is actually no sensible reason on earth to use any JS stuff past ES5, all they have done past that point is messing the language up. Julio
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2019-02-17 12:50 +1100 |
| Message-ID | <R72dnZ_OYfxzIPXBnZ2dnUU7-dXNnZ2d@westnet.com.au> |
| In reply to | #36026 |
On 17/02/2019 5:43 am, Julio Di Egidio wrote: > FWIW, I'd give the exact opposite advice: there is actually no sensible reason > on earth to use any JS stuff past ES5, all they have done past that point is > messing the language up. I realise that you probably know more about ES than I do. Still this <http://exploringjs.com/es6/ch_overviews.html> looks good. What is messing up the language? Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2019-02-17 16:23 +0100 |
| Message-ID | <2782d585-db51-4060-a3e2-6607222a5fd9@PointedEars.de> |
| In reply to | #36027 |
Andrew Poulos wrote: > On 17/02/2019 5:43 am, Julio Di Egidio wrote: >> FWIW, I'd give the exact opposite advice: there is actually no sensible reason >> on earth to use any JS stuff past ES5, all they have done past that point is >> messing the language up. > > I realise that you probably know more about ES than I do. Your realization is wrong. The records shows that Julio Di Egidio is just a wannabe and a troll; he has a big mouth and no clue. > Still this > <http://exploringjs.com/es6/ch_overviews.html> > looks good. What is messing up the language? Nothing. Do not feed the troll. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-02-17 15:41 -0800 |
| Message-ID | <abd5f73d-6c10-461c-a31e-1ea71570e96d@googlegroups.com> |
| In reply to | #36028 |
On Sunday, 17 February 2019 16:23:23 UTC+1, Thomas 'PointedEars' Lahn wrote: > The records shows that Julio Di Egidio is just a > wannabe and a troll; he has a big mouth and no clue. You are a fraud and disgrace for this arena. But no pearls for swine, keep guessing... *Plonk* Julio
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-02-17 16:08 -0800 |
| Message-ID | <ce3a8d95-e0f6-476b-843b-1a648efa0f89@googlegroups.com> |
| In reply to | #36027 |
[Restored context: the resident troll and moron must exposed.] On Saturday, 16 February 2019 16:37:10 UTC+1, John G Harris wrote: > On Fri, 15 Feb 2019 15:08:27 +0100, Thomas 'PointedEars' Lahn > <PointedEars@web.de> wrote: > <snip> > >Replace “var” with “let” or “const” in production-quality code (unless it > >must be backwards-compatible to ES < 6 and you are not using a transpiler, > >in which case you must replace the other expressions as well). > > Also, you can't create an object with an empty prototype chain if > using ES < 5. On Sunday, 17 February 2019 02:50:49 UTC+1, Andrew Poulos wrote: > On 17/02/2019 5:43 am, Julio Di Egidio wrote: > > > FWIW, I'd give the exact opposite advice: there is actually no sensible reason > > on earth to use any JS stuff past ES5, all they have done past that point is > > messing the language up. > > I realise that you probably know more about ES than I do. Still this > <http://exploringjs.com/es6/ch_overviews.html> > looks good. What is messing up the language? Looks good by which standard, you utter moron? Rather learn to use question marks honestly, MORON. And take note: I don't give a shit about teaching the unteachable... *Plonk* Julio
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2019-02-18 11:32 +1100 |
| Message-ID | <8PGdnZXcBKGdYPTBnZ2dnUU7-VednZ2d@westnet.com.au> |
| In reply to | #36031 |
On 18/02/2019 11:08 am, Julio Di Egidio wrote: > And take note: I don't give a shit about teaching the unteachable... Ha ha ha, repeating what your teachers told you. Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-02-17 22:13 -0800 |
| Message-ID | <94005f19-18bd-40bb-b488-e67a1e3ff8eb@googlegroups.com> |
| In reply to | #36032 |
On Monday, 18 February 2019 01:32:09 UTC+1, Andrew Poulos wrote: > On 18/02/2019 11:08 am, Julio Di Egidio wrote: > > > And take note: I don't give a shit about teaching the unteachable... > > Ha ha ha, repeating what your teachers told you. Hahaha, how old are you, 6? Laugh at your lack of integrity, you stupid fuck. That said, a thread about how to do a loop?! You stupid fucks must be truly retarded... Julio
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2019-02-18 19:56 +1100 |
| Message-ID | <6cSdnYMF-Z3N7vfBnZ2dnUU7-TednZ2d@westnet.com.au> |
| In reply to | #36033 |
On 18/02/2019 5:13 pm, Julio Di Egidio wrote: > That said, a thread about how to do a loop?! You stupid fucks must be truly > retarded... Don't hold back. Let everyone know what kind of person you really are (just in case there's someone with any doubt). Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-02-18 01:24 -0800 |
| Message-ID | <6988ba0a-ec61-41b1-83c8-1b8d46d4477a@googlegroups.com> |
| In reply to | #36034 |
On Monday, 18 February 2019 09:56:58 UTC+1, Andrew Poulos wrote: > On 18/02/2019 5:13 pm, Julio Di Egidio wrote: > > > That said, a thread about how to do a loop?! You stupid fucks must be truly > > retarded... > > Don't hold back. Let everyone know what kind of person you really are > (just in case there's someone with any doubt). Everyone who, you delusional moron and patent troll who can't even really google stuff up, you and the other resident trolls?? This group is rather dead thanks to you resident fucks. Why don't you all just migrate to SO for good?? Indeed, why don't you get another hobby, say, in agriculture? (EOD.) *Plonk* Julio
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2019-02-18 20:36 +1100 |
| Message-ID | <nuednTXtYpg54ffBnZ2dnUU7-eHNnZ2d@westnet.com.au> |
| In reply to | #36035 |
On 18/02/2019 8:24 pm, Julio Di Egidio wrote: > *Plonk* If you are describing yourself the correct term is *Plonker*. Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2019-02-19 19:15 -0800 |
| Message-ID | <dd062070-38eb-456c-8510-f83016cbf013@googlegroups.com> |
| In reply to | #36025 |
John G Harris wrote: > Also, you can't create an object with an empty prototype chain if > using ES < 5. Unless you abuse the host environment in some way. For example, across an iframe boundary: <https://stackoverflow.com/a/36117912/153209>
[toc] | [prev] | [next] | [standalone]
| From | Mickey Finn <mfinney@gmail.com> |
|---|---|
| Date | 2019-02-17 13:42 -0300 |
| Message-ID | <q4c2t5$15v8$1@gioia.aioe.org> |
| In reply to | #36020 |
Andrew Poulos wrote:
> If I have an object that looks like this
>
> {
> "links": [
> {
> "from": "1ff9fce8",
> "to": "2cc8ebd7",
> "id": "1a8d67fe"
> },
> {
> "from": "2cc8ebd7",
> "to": "8d1d8c2b",
> "id": "f4ca15f5"
> },
> {
> "from": "2cc8ebd7",
> "to": "4c48567f",
> "id": "9384e250"
> },
> {
> "from": "8d1d8c2b",
> "to": "9e2e9d3c",
> "id": "12c2b1b4"
> },
> {
> "from": "2cc8ebd7",
> "to": "324db009",
> "id": "8c348628"
> },
> {
> "from": "4c48567f",
> "to": "9e2e9d3c",
> "id": "2cbe2766"
> },
> {
> "from": "324db009",
> "to": "9e2e9d3c",
> "id": "e715fe9f"
> },
> ...
> ]
> }
>
> how can I make a list of the number of times a 'from' value that appears
> more than once appears?
>
> In the example above "2cc8ebd7" appears as 'from' 3 times. I've got some
> code with nested for loops that sort of works but I'm struggling to work
> out a more robust way.
var o = {
"links": [
{
"from": "1ff9fce8",
"to": "2cc8ebd7",
"id": "1a8d67fe"
},
/* etc. */
]
};
var a = o.links;
var b = a.map(x => x.from);
var c = b.reduce((a, c) => a.set(c, 1 + (a.get(c) || 0)), new Map());
console.log(c);
//Yields a Map():
{"1ff9fce8" => 1, "2cc8ebd7" => 3, "8d1d8c2b" => 1, "4c48567f" => 1,
"324db009" => 1}
[toc] | [prev] | [next] | [standalone]
| From | dr.j.r.stockton@gmail.com |
|---|---|
| Date | 2019-02-26 07:43 -0800 |
| Message-ID | <49c72c15-663b-4154-bd6f-b71637fa73ff@googlegroups.com> |
| In reply to | #36020 |
On Friday, 15 February 2019 08:44:43 UTC, Andrew Poulos wrote: > If I have an object that looks like this > ... > how can I make a list of the number of times a 'from' value that appears > more than once appears? Firstly : The FIRST thing to do is to consider just who, and what software, will be running your code. If this is a private HTML page just for you, your current browser, and your current OS, then anything which works for that will do - though you should write in a subset of HTML which you can comfortably read. If it is for a knowable group of fellow-employees, for example, you need to be somewhat more careful. If it is for the Web, more careful than that. And so on. For example, I have J[ava]Script which can be used at a Windows command line with CScript, which cannot sensibly be used _as_is_ with WScript, and can also be used as an include file by a .HTA - for all of those it must be JScript-compatible. It can also be used by a modern standard browser, so HTML5 applies; but it must also be easily readable by me, so it must as far as practical be HTML3. J[ava]Script can be embedded in a system which is not primarily a computer as such - e.g. a router, a measuring instrument, a car, a satellite ... Code to be used non-commercially by myself and friends should be moderately foolproof, but not necessarily idiot-proof; but code for commercial use on the Web needs to be bullet-proof. And so on. So you need to say something about the circumstances under which the code may be run, at least to show that you have thought about that. Secondly: Your shown values are all strings of eight characters representing hexadecimal digits; there *might* be advantage in using 32-bit unsigned integers instead. Thirdly: I have for some years now been storing (local, relative) Windows file names as the identifiers of properties of an ordinary object, with the data being the mere existence of a property or additionally its integer value - and that is very like what has already been suggested here. It works exactly as hoped for, in CScript and in Firefox. Tests seem to show that it works also with strings which could not be such names - but I've not tested all possible strings. -- (c) John Stockton, near London, UK. Using Google Groups. | Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web