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


Groups > comp.lang.javascript > #36029

Re: getting number of prop values from array

From Mickey Finn <mfinney@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: getting number of prop values from array
Date 2019-02-17 13:42 -0300
Organization Aioe.org NNTP Server
Message-ID <q4c2t5$15v8$1@gioia.aioe.org> (permalink)
References <FfudnXxbaMVs5vvBnZ2dnUU7-f_NnZ2d@westnet.com.au>

Show all headers | View raw


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}




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


Thread

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

csiph-web