基于多个键对 Json 进行分组
Posted
技术标签:
【中文标题】基于多个键对 Json 进行分组【英文标题】:Grouping Json Based on Multiple Keys 【发布时间】:2015-05-06 10:44:54 【问题描述】:我有一个像下面这样的 Json 结构
[
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I1",
"item_name": "C1_I1_Name",
"variant_id": "C1_I1_V1",
"variant_name": "C1_I1_V1_ Name"
,
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I1",
"item_name": "C1_I1_Name",
"variant_id": "C1_I1_V2",
"variant_name": "C1_I1_V2_ Name"
,
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I2",
"item_name": "C1_I2_Name",
"variant_id": "C1_I2_V1",
"variant_name": "C1_I2_V1_ Name"
,
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I2",
"item_name": "C1_I2_Name",
"variant_id": "C1_I2_V2",
"variant_name": "C1_I2_V2_ Name"
,
"category_id": "C2",
"category_name": "C2_Name",
"item_id": "C2_I1",
"item_name": "C2_I1_Name",
"variant_id": "C2_I1_V1",
"variant_name": "C2_I1_V1_ Name"
]
我想在 javascript 中将 Json 分组为 N' 级别,例如通过传递键列表。例如给出 category_id、item_id 和 variant_id,我期待以下响应:
[
"category_id": "C1",
"category_name": "C1_Name",
"item": [
"item_id": "C1_I1",
"item_name": "C1_I1_Name",
"variant": [
"variant_id": "C1_I1_V1",
"variant_name": "C1_I1_V1_ Name"
,
"variant_id": "C1_I1_V2",
"variant_name": "C1_I1_V2_ Name"
]
,
"item_id": "C1_I2",
"item_name": "C1_I2_Name",
"variant": [
"variant_id": "C1_I2_V1",
"variant_name": "C1_I2_V1_ Name"
,
"variant_id": "C1_I2_V2",
"variant_name": "C1_I2_V2_ Name"
]
]
]
需要有关如何解决此问题的帮助吗?
我尝试了类似以下的方法,但它为一个级别分组。我希望它是递归的。
function groupBy(collection, property)
var i = 0, val, index, values = [], result = [];
for (; i < collection.length; i++)
val = collection[i][property];
index = values.indexOf(val);
if (index > -1)
result[index].push(collection[i]);
else
values.push(val); result.push([collection[i]]);
return result;
【问题讨论】:
我尝试了类似以下的方法,但它为一个级别分组,我希望它是递归的 [code]function groupBy(collection, property) var i = 0, val, index, values = [],结果 = []; for (; i -1) result[index].push(collection[i]);否则 values.push(val);结果.push([collection[i]]); 返回结果; 您要传递键名或值的列表吗? 键名(在上面的例子中,我需要传递 category_id、item_id、variant_id 作为列表 【参考方案1】:这是我的任务解决方案(仅限 Javascript):
function getItemByKey(arr, key, value)
// here we look for existing group item in the result array
return arr.reduce(function (prev, cur)
if (prev == null && cur[key] == value)
return cur;
return prev;
, null);
function checkPropForGroup(prop, key)
// here we check which columns should stay in the group,
// and which should go down to group items.
return prop == key || prop == key.replace("_id", "_name");
function cloneSuperItem(src, key)
// create super item by copying only group related fields
var item = ;
for (prop in src)
if (checkPropForGroup(prop, key))
item[prop] = src[prop];
item[key+'_group'] = [];
return item;
function cloneSubItem(src, key)
// create sub-item by copying all but group related fields
var item = ;
for (prop in src)
if (!checkPropForGroup(prop, key))
item[prop] = src[prop];
return item;
function groupArray(arr, args, lvl)
var key = args[lvl];
var result = [];
arr.forEach(function (item, ind, a)
// find or create super item for group and then create sub item
// and push it there
var keyItem = getItemByKey(result, key, item[key]);
if (!keyItem)
keyItem = cloneSuperItem(item, key);
result.push(keyItem);
subItem = cloneSubItem(item, key);
keyItem[key+'_group'].push(subItem);
);
if (args[lvl+1])
// recursively make grouping on lower level
for (var i = 0; i < result.length; i++)
result[i][key+'_group'] =
groupArray(result[i][key+'_group'], args, lvl + 1);
return result;
function groupArrayMain(arr, keysArray)
// keys should be simply listed as parameters in order from top group to lower
return JSON.stringify(groupArray(arr, arguments, 1),null,'\n')
var arr = [
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I1",
"item_name": "C1_I1_Name",
"variant_id": "C1_I1_V1",
"variant_name": "C1_I1_V1_ Name"
,
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I1",
"item_name": "C1_I1_Name",
"variant_id": "C1_I1_V2",
"variant_name": "C1_I1_V2_ Name"
,
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I2",
"item_name": "C1_I2_Name",
"variant_id": "C1_I2_V1",
"variant_name": "C1_I2_V1_ Name"
,
"category_id": "C1",
"category_name": "C1_Name",
"item_id": "C1_I2",
"item_name": "C1_I2_Name",
"variant_id": "C1_I2_V2",
"variant_name": "C1_I2_V2_ Name"
,
"category_id": "C2",
"category_name": "C2_Name",
"item_id": "C2_I1",
"item_name": "C2_I1_Name",
"variant_id": "C2_I1_V1",
"variant_name": "C2_I1_V1_ Name"
]
groupArrayMain(arr, "category_id", "item_id", "variant_id")
我也把它写成jsfiddle
【讨论】:
以上是关于基于多个键对 Json 进行分组的主要内容,如果未能解决你的问题,请参考以下文章
用java中的键对Json响应进行分组 - android studio
pandas使用groupby函数基于多个分组变量(多变量分组)对dataframe数据进行分组使用get_group函数获取指定组合多分组变量对应的样本(使用元组指定多个分组变量)