如何按长度划分分组数组对象?
How can I divide grouped array-object by length?
我从数据库中检索了以下数据
var members = [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
];
第一步是按部门名称对这些数据进行分组。
var groupedMembers = _.groupBy(members, "department_name");
↓
{
"aaa" : [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" }
],
"bbb" : [
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
]
}
从这个状态开始,我想进一步将对象拆分为每三个对象。
理想情况下,我希望按以下方式格式化数组
var expected = {
"aaa1" : [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" }
],
"aaa2" : [
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" }
],
"bbb" : [
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
]
};
我可以使用 Underscore.js 或 lodash.js 解决这个问题吗?
如果不是太难,你可以使用其他库或纯 javascript 实现。
谢谢。
您可以只使用纯 javascript 来实现。
var groupedMembers = {
"aaa" : [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" }
],
"bbb" : [
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
]
}
var splitByThree = {}
for (const key in groupedMembers) {
if (groupedMembers[key].length <= 3) {
splitByThree[key] = groupedMembers[key]
}
else {
for (var i=0, j=groupedMembers[key].length; i<j; i+=3) {
splitByThree[key + (i+3)/3] = groupedMembers[key].slice(i, i+3);
}
}
}
那么 splitByThree 就是您的预期结果。
我从数据库中检索了以下数据
var members = [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
];
第一步是按部门名称对这些数据进行分组。
var groupedMembers = _.groupBy(members, "department_name");
↓
{
"aaa" : [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" }
],
"bbb" : [
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
]
}
从这个状态开始,我想进一步将对象拆分为每三个对象。
理想情况下,我希望按以下方式格式化数组
var expected = {
"aaa1" : [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" }
],
"aaa2" : [
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" }
],
"bbb" : [
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
]
};
我可以使用 Underscore.js 或 lodash.js 解决这个问题吗? 如果不是太难,你可以使用其他库或纯 javascript 实现。
谢谢。
您可以只使用纯 javascript 来实现。
var groupedMembers = {
"aaa" : [
{ "name" : "a", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "b", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "c", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "d", "department_id" : 1, "department_name" : "aaa" },
{ "name" : "e", "department_id" : 1, "department_name" : "aaa" }
],
"bbb" : [
{ "name" : "f", "department_id" : 2, "department_name" : "bbb" },
{ "name" : "g", "department_id" : 2, "department_name" : "bbb" }
]
}
var splitByThree = {}
for (const key in groupedMembers) {
if (groupedMembers[key].length <= 3) {
splitByThree[key] = groupedMembers[key]
}
else {
for (var i=0, j=groupedMembers[key].length; i<j; i+=3) {
splitByThree[key + (i+3)/3] = groupedMembers[key].slice(i, i+3);
}
}
}
那么 splitByThree 就是您的预期结果。