javascript - Grouping JSON by values -


using lever job posting api, i'm getting json results sorted location, , i'm trying figure out how group results "team" within results, shopify careers page.

here codepen , here json

i tried adding following on line 38 of codepen try grab team values, it's not outputting expected (i'm getting 1 letter per line, isn't helpful):

for (var x in _data[i].postings[j].categories.team) 

i'm sure it's super simple, i'm not javascript guy. appreciated!

assume , json output is

outjson=   [ {       team: "teama",       name: "ahmed",       field3:"val3"   },  {       team: "teamb",       name: "ahmed",       field3:"val43"   },  {       team: "teama",       name: "ahmed",       field3:"val55"   },    ] 

then see groupby function in demo below:


demo :

outjson= [ {team: "teama",name: "ahmed",field3:"val3"}, {team: "teamb",name: "ahmed",field3:"val43"}, {team: "teama",name: "ahmed",field3:"val55"} ]    var groupby = function(xs, key) {    return xs.reduce(function(rv, x) {      (rv[x[key]] = rv[x[key]] || []).push(x);      return rv;    }, {});  };  var groubedbyteam=groupby(outjson, 'team')  console.log(groubedbyteam);

then , if want loop through categories (teams), categories in array :

object.keys(groubedbyteam) // return ["teama","teamb"] 

then :

  object.keys(groubedbyteam).foreach(function(category){         console.log(`team ${category} has ${groubedbyteam[category].length} members : `);         groubedbyteam[category].foreach(function(memb,i){               console.log(`---->${i+1}. ${memb.name}.`)        })   });  

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -