我有一个下面的对象,我试图使用部门和子部门值对数据进行分组。我尝试在部门名称上使用reduce功能但无法达到预期的效果。
例如,我有一系列部门对象:
var arr = //Actual Object
[{
"department": "Admin",
"sub_department": [{
"name": "Finance",
"application": [{
"name": "F1",
"tag": 100
}]
}]
},
{
"department": "Admin",
"sub_department": [{
"name": "Registry",
"application": [{
"name": "R2",
"tag": 200
}]
}]
},
{
"department": "Admin",
"sub_department": [{
"name": "Finance",
"application": [{
"name": "F2",
"tag": 200
}]
}]
},
{
"department": "Helpdesk",
"sub_department": [{
"name": "Entry",
"application": [{
"name": "E1",
"tag": 200
}]
}]
}
]
//Tried below code
var result = arr.reduce(function(r, a) {
r[a.department] = r[a.department] || [];
r[a.department].push(a);
return r;
}, Object.create(null));
console.log(result)

我试图通过对同一个部门进行分组来实现所需的结果,如果department和sub_department.name相同,则将应用程序推送到sub_department数组。
[
{
"department": "Admin",
"sub_department": [
{
"name": "Finance",
"application": [
{
"name": "F1",
"tag": 100
},
{
"name": "F2",
"tag": 200
}
]
},
{
"name": "Registry",
"application": [
{
"name": "R2",
"tag": 200
}
]
}
]
},
{
"department": "Helpdesk",
"sub_department": [
{
"name": "Entry",
"application": [
{
"name": "E1",
"price": 200
}
]
}
]
}
]
对此的任何帮助都会非常有用。
答案 0 :(得分:1)
你其实很亲密。只需将对象构建为哈希表值,并使用Object.values
将哈希表转换为数组:
var result = Object.values(arr.reduce(function(r, a) { //<--- the hashtable > array conversion
r[a.department] = r[a.department] || {
department: a.department,
sub_department:[]
}; // <-- objects instead of arrays in the hashtable
r[a.department].sub_department.push(...a.sub_department); //and add the subdepartment not the object itself
return r;
}, Object.create(null)));
console.log(result);
或者你可以并行构建一个哈希表和结果数组,这就是我如何做到的:
const result = [], hash = {};
for(const {department, sub_department} of arr){
if(hash[department]){
hash[department].push(...sub_department);
} else {
result.push({department, sub_department});
hash[department] = sub_department;
}
}
还要对sub_departments进行分组,使其稍微复杂一些。为此,我们需要一个嵌套的哈希表和一个嵌套查找:
const exit = Symbol("exit");
const result = [], hash = {};
for(const {department, sub_department} of arr){
if(!hash[department]){
const sub = [];
hash[department] = {[exit]: sub};
result.push({department, sub_department: sub});
}
for(const {name, ...data} of sub_department){
if(hash[department][name]){
hash[department][name].push(data);
} else {
const subsub = [];
hash[department][exit].push({name, subsub});
hash[department][name] = subsub;
}
}
}
答案 1 :(得分:0)
扩展先前发布的解决方案的一个有点冗长的解决方案:
objMsg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.gmail.com"
objMsg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
objMsg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
objMsg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMsg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
objMsg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="myemail@mailserver.in"
objMsg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="mypassword"
objMsg.Configuration.Fields.Update
objMsg.From = "myemail@myserver.in"
objMsg.To = "jojysp@gmail.com"
objMsg.Subject = mSub
objMsg.HTMLBody = mText
objMsg.Send
Set objMsg = Nothing