我有一个我想基于“区域”字段解析的 json,以便通过选择输入获取区域列表等。
var data = {
"zone01": [{
"region": "North",
"state": "New York",
"county": "Albany",
"code": "01"
}, {
"region": "North",
"state": "New York",
"county": "Albany",
"code": "05"
}, {
"region": "North",
"state": "Maine",
"county": "Auburn",
"code": "07"
}],
"zone02": [{
"region": "South",
"state": "Florida",
"county": "Gainseville",
"code": "82"
}, {
"region": "South",
"state": "Florida",
"county": "Macclenny",
"code": "73"
}]
};
我可以使用以下方法解析它:
function setValues(range, zone, region, state, country){
if(range === 'zone'){
var getRegions = _.groupBy(data.zone02, 'region');
$.each(getRegions, function(k, v){
var region = JSON.stringify(k);
region = region.replace(/"/g,'');
$('select#region').append('<option value="'+region+'">'+region+'</option>');
});
}
}
但我真正需要的是 _.groupBy(data.zone02, 'region') 与 data.zone02 是数据 + 函数的变量区
更新
这是我的成品,没有可读性和可重用性:jsFiddle