web-dev-qa-db-ja.com

jqueryを使用してjsonデータのグループ化

テンプレートのデータを動的に提供できるように、部門ごとにjson応答をフォーマットしたいと思います。コードは、データ取得jSONを使用し、Imはそこでスタックします。データを個別に引き出すことができますが、jquery/javascriptを使用してこれを実行できるかどうか知りたいです。また、これを実現するためにUnderscore.jsというユーティリティライブラリがあることも読みました。

[{
    "division": "East",
    "rm_name": "Russ Martin",
    "address": "MT,VT, NH, ME  (all firms)",
    "state": "MT",
    "coordinates": "43.299428,-74.217933"
}, {
    "division": "East",
    "rm_name": "Carey Fischer",
    "address": "NY- Upstate ex Rockland County (BD, FP)",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Brandon Born",
    "address": "CT, NY - Upstate, MA - ex metro Boston (FI), MA - Central, West (all firms)",
    "state": "CT",
    "coordinates": "40.714353,-74.005973"
}, {
    "division": "East",
    "rm_name": "Joe Tocyloski",
    "address": "PA - East, NJ - South (FP)",
    "state": "PA",
    "coordinates": "41.603221,-73.087749"
}, {
    "division": "East",
    "rm_name": "Phil Hemery",
    "address": "NJ (FI), NJ - Bergen County (all firms), NY - NYC (FI,RIA)/Westchester (FI,BD)/Rockland County (all firms)",
    "state": "NJ",
    "coordinates": "41.203456,-77.189941"
}, {
    "division": "East",
    "rm_name": "Bob Mancini",
    "address": "MA - East (all firms)",
    "state": "MA",
    "coordinates": "42.407235,-71.383667"
}, {
    "division": "East",
    "rm_name": "Damien Ramondo",
    "address": "NJ, PA East",
    "state": "NJ",
    "coordinates": "41.203456,-77.189941"
}, {
    "division": "East",
    "rm_name": "Kevin Gang",
    "address": "W VA,PA - West (all firms), OH - Cleveland (BD, FI)",
    "state": "VA",
    "coordinates": "44.314844,-85.602364"
}, {
    "division": "East",
    "rm_name": "Andrew Fischer",
    "address": "MI - (all firms)",
    "state": "MI",
    "coordinates": "40.057052,-74.404907"
}, {
    "division": "East",
    "rm_name": "David Saslowsky",
    "address": "NYC",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Robert Brazofsky",
    "address": "NYC",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Joseph Proscia",
    "address": "NJ - North ex Bergen County, NY - NYC (FP)",
    "state": "NJ",
    "coordinates": "41.203456,-77.189941"
}, {
    "division": "East",
    "rm_name": "William Marsalise",
    "address": "NY - LI and Outer Boroughs (FI), LI Planners",
    "state": "NY",
    "coordinates": "46.879682,-110.362566"
}, {
    "division": "East",
    "rm_name": "Dan Stack",
    "address": "OH - Columbus and South (all firms), KY - Covington (all firms)",
    "state": "OH",
    "coordinates": "37.439974,-78.662109"
}, {
    "division": "East",
    "rm_name": "James Broderick",
    "address": "OH - North of Columbus ex Cleveland (all firms), Cleveland (FP)",
    "state": "OH",
    "coordinates": "37.439974,-78.662109"
}, {
    "division": "South",
    "rm_name": "Chris Carrelha",
    "address": "FL - North (FP)",
    "state": "FL",
    "coordinates": "40.417287,-82.907123"
}, {
    "division": "South",
    "rm_name": "Don Connell",
    "address": "TN, AR, KY - ex Covington (all firms), MO - St. Louis (by firm)",
    "state": "TN",
    "coordinates": "27.664827,-81.515754"
}, {
    "division": "South",
    "rm_name": "Jay O'Connor",
    "address": "NC - ex coast, SC, GA - Savannah to Augusta (all firms)",
    "state": "NC",
    "coordinates": "35.517491,-86.580447"
}, {
    "division": "South",
    "rm_name": "Dwight Cornell",
    "address": "FL - South, VI, PR (all firms)",
    "state": "FL",
    "coordinates": "40.417287,-82.907123"
}, {
    "division": "South",
    "rm_name": "Eric Indovina",
    "address": "AL (all firms), GA (BD, RIA)",
    "state": "AL",
    "coordinates": "14.97198,19.753418"
}, {
    "division": "South",
    "rm_name": "Russ Corby",
    "address": "TX - DFW (BD, FP) Houston Austin and San Antonio (FP, FI)",
    "state": "TX",
    "coordinates": "32.318231,-86.902298"
}, {
    "division": "South",
    "rm_name": "Chris Boeker",
    "address": "TX - Southeast (BD), MS, LA - South (all firms)",
    "state": "TX",
    "coordinates": "32.318231,-86.902298"
}, {
    "division": "South",
    "rm_name": "Robert Nelms",
    "address": "VA - ex DC metro (all firms), NC - Coast, MD (ex Baltimore), VA, DC (FI), TN - northeastern corner (all firms)",
    "state": "VA",
    "coordinates": "44.314844,-85.602364"
}, {
    "division": "South",
    "rm_name": "Joe Dominguez",
    "address": "FL - North (BD, FI)",
    "state": "FL",
    "coordinates": "40.417287,-82.907123"
}, {
    "division": "South",
    "rm_name": "Marc Della Pia",
    "address": "MD, DC - Metro, VA - Alexandria  (BD, FA), Baltimore (FI)",
    "state": "MD",
    "coordinates": "31.968599,-99.901813"
}, {
    "division": "South",
    "rm_name": "Chris Carrelha",
    "address": "GA (FI, FP)",
    "state": "GA",
    "coordinates": "32.166313,-82.902832"
}, {
    "division": "South",
    "rm_name": "Terry Harris",
    "address": "OK, TX - by city (all firms)",
    "state": "OK",
    "coordinates": "32.166313,-82.902832"
}, {
    "division": "West",
    "rm_name": "Arend Elston",
    "address": "AR, MO, IL - South(all firms)",
    "state": "AR",
    "coordinates": "35.007752,-97.092877"
}, {
    "division": "West",
    "rm_name": "John Schmidt",
    "address": "IL - Chicago Metro (BD)",
    "state": "IL",
    "coordinates": "35.20105,-91.831833"
}, {
    "division": "West",
    "rm_name": "Jason Stevens",
    "address": "MN, ND (All Firms)",
    "state": "MN",
    "coordinates": "46.7248,-94.680176"
}, {
    "division": "West",
    "rm_name": "Laura Channell",
    "address": "IL - Chicago Metro (FI, FP)",
    "state": "IL",
    "coordinates": "35.20105,-91.831833"
}, {
    "division": "West",
    "rm_name": "Patrick Denis",
    "address": "WI (all firms), Upper Peninsula of Michigan",
    "state": "WI",
    "coordinates": "46.729553,-94.6859"
}, {
    "division": "West",
    "rm_name": "Michael Cheskis",
    "address": "IN (all firms), IL - Chicago Metro (By Firm)",
    "state": "IN",
    "coordinates": "43.78444,-88.787868"
}, {
    "division": "West",
    "rm_name": "Dave Mitchell",
    "address": "IA, KS, NE, SD, MO - Kansas City (all firms)",
    "state": "IA",
    "coordinates": "40.271144,-86.132812"
}, {
    "division": "West",
    "rm_name": "Paul Moyer",
    "address": "WA, ID, OR (all firms)",
    "state": "WA",
    "coordinates": "41.877741,-93.098145"
}, {
    "division": "West",
    "rm_name": "Peter Szabo",
    "address": "AK, NV - Reno (all firms), N. CA (BD & FI), San Francisco (FI)",
    "state": "AK",
    "coordinates": "47.751074,-120.740139"
}, {
    "division": "West",
    "rm_name": "Lou Tousignant",
    "address": "N.CA (FP), San Francisco (BD & FP)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Matt Malvey",
    "address": "CA - San Diego (all firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Kevin Dausch",
    "address": "N. CA (BD & FI), San Francisco (FI)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Scott Hutton",
    "address": "CO, NM, WY, MT (all firms)",
    "state": "Colorado",
    "coordinates": "40.747164,-74.000566"
}, {
    "division": "West",
    "rm_name": "Brian Buehring",
    "address": "CA - North LA to Santa Barbara, HI (all firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Robert Forrester",
    "address": "CA - Orange County to San Diego (all firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}, {
    "division": "West",
    "rm_name": "Mike Ossmen",
    "address": "CA - Downtown LA, Pasadena, South Bay & Inland Empire (All Firms)",
    "state": "CA",
    "coordinates": "64.200841,-149.493673"
}]

Jqueryを使用してこれを実行できますか?任意の提案を事前に感謝します。

11
a_miguel6687

アンダースコアで簡単にできます。

var groupedData = _.groupBy(data, function(d){return d.division});

フィドル

18

下線なしでも Array.prototype.reduce このように

console.log(data.reduce(function(result, current) {
    result[current.division] = result[current.division] || [];
    result[current.division].Push(current);
    return result;
}, {}));
31
thefourtheye

ここで同じ質問:

車の配列を色でグループ化するためにアンダースコアgroupbyを使用

アンダースコアでグループ化し、HTMLテンプレートで最後のjsonを使用できます。

var g = _.groupBy(JsonVar, 'division');

console.log(JSON.stringify(g));