2014-04-09 8 views
9

Chciałbym sformatować moją odpowiedź json według działu, aby móc dynamicznie dostarczać dane do mojego szablonu. kod użyje get jSON danych, a następnie utknąłem tam. Mogę wyciągnąć dane indywidualnie, ale jestem ciekawy, czy mogę to zrobić za pomocą jquery/javascript. Przeczytałem również, że istnieje biblioteka narzędzi o nazwie Underscore.js, aby to osiągnąć.Grupuj według danych json za pomocą jquery

[{ 
    "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" 
}] 

Czy mogę to zrobić za pomocą jquery? Z góry dziękuję za wszelkie sugestie.

+0

Jak to _format_ JSON? Masz na myśli zmianę kolejności/filtrowanie? – Andy

+0

Jakie jest oczekiwane wyjście ? – thefourtheye

+0

Tak, chcę zmienić kolejność według podziału, aby wszystkie osoby w Oddziale Wschodnim zostały zgrupowane i tak dalej. –

Odpowiedz

14

Możesz to zrobić, podkreślając tak łatwo.

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

Fiddle

24

Można to zrobić bez podkreślenia Również z Array.prototype.reduce jak ten

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