使用jquery为json数据分组

时间:2014-04-09 11:43:12

标签: javascript jquery underscore.js

我想通过Division格式化我的json响应,以便我可以动态地为我的模板提供数据。代码将使用获取数据jSON然后我卡在那里。我可以单独提取数据,但我很好奇,如果我可以使用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吗?提前感谢任何建议。

3 个答案:

答案 0 :(得分:29)

你可以在没有下划线的情况下这样做,Array.prototype.reduce就像这样

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

答案 1 :(得分:17)

你可以通过下划线轻松完成。

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

Fiddle

答案 2 :(得分:2)

同样的问题:

Using underscore groupby to group an array of cars by their colour

您可以使用下划线进行分组,然后将最终的json与HTML模板一起使用。

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

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