如何从对象的对象中获得最详细的唯一对象组合?

  • 本文关键字:对象 唯一 组合 javascript
  • 更新时间 :
  • 英文 :


我有一个公司层次结构选择对象的对象,例如:

{
1:   { division: "division1" },
2:   { division: "division2" },
11:  { division: "division2", branch: "branch2" },
41:  { division: "division2", branch: "branch2", department: "department2" },
100: { division: "division2", branch: "branch2", department: "department10" },
102: { division: "division2", branch: "branch3" },
130: { division: "division17" },
144: { division: "division17" , branch: "branch22" },
200: { division: "division50" }
}

我需要得到唯一的对象,但最详细的对象。所以上面的例子应该是:

{
1:   { division: "division1" },
41:  { division: "division2", branch: "branch2", department: "department2" },
100: { division: "division2", branch: "branch2", department: "department10" },
102: { division: "division2", branch: "branch3" },
144: { division: "division17" , branch: "branch22" },
200: { division: "division50" }
}

这些是从3个下拉菜单中选择的,每个选择包含到该项目的整个层次结构的数据(层次结构是division>branch祝辞department)。因此,如果用户选择division2然后branch2,它将添加{ division: "division2" }{ division: "division2", branch: "branch2" },但我只需要最详细的{ division: "division2", branch: "branch2" }

*编辑:没有必要保留原来的键

我该怎么做呢?

您可以使用已知值构建树,并通过查找值或检查属性的长度来筛选数组。

const
data = { 1: { division: "division1" }, 2: { division: "division2" }, 11: { division: "division2", branch: "branch2" }, 41: { division: "division2", branch: "branch2", department: "department2" }, 100: { division: "division2", branch: "branch2", department: "department10" }, 100: { division: "division2", branch: "branch3" }, 130: { division: "division17" }, 144: { division: "division17", branch: "branch22" }, 200: { division: "division50" } },
keys = ['division', 'branch', 'department'],
tree = Object.values(data).reduce((r, o) => {
keys.reduce((q, k) => o[k] && (q[o[k]] ??= {}), r);
return r;
}, {}),
result = Object
.values(data)
.filter(o => {
let t = tree;
return keys.every(k => {
if (o[k] in t) return t = t[o[k]];
return !Object.keys(t).length;
});
});
console.log(result);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

如果您不需要原始对象,您可以从树中构建结果集。

const
getObjects = (tree, [key, ...keys]) => {
const entries = Object.entries(tree);
return entries.length
? entries.flatMap(([k, t]) => getObjects(t, keys).map(o => ({ [key]: k, ...o })))
: [{}];
},
data = { 1: { division: "division1" }, 2: { division: "division2" }, 11: { division: "division2", branch: "branch2" }, 41: { division: "division2", branch: "branch2", department: "department2" }, 100: { division: "division2", branch: "branch2", department: "department10" }, 100: { division: "division2", branch: "branch3" }, 130: { division: "division17" }, 144: { division: "division17", branch: "branch22" }, 200: { division: "division50" } },
keys = ['division', 'branch', 'department'],
tree = Object.values(data).reduce((r, o) => {
keys.reduce((q, k) => o[k] && (q[o[k]] ??= {}), r);
return r;
}, {}),
result = getObjects(tree, keys);
console.log(result);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

let a = {
1: { division: "division1" },
2: { division: "division2" },
11: { division: "division2", branch: "branch2" },
41: { division: "division2", branch: "branch2", department: "department2" },
100: { division: "division2", branch: "branch2", department: "department10" },
102: { division: "division2", branch: "branch3" },
130: { division: "division17" },
144: { division: "division17", branch: "branch22" },
200: { division: "division50" }
}
const aList = Object.values(a) 

// 1. save all into Map by id
const map = new Map();
for (let value of aList) {
const id = `${value.division} / ${value.branch} / ${value.department}`;
map.set(id, value);
}
// 2. remove duplicates
for (let value of aList) {
const id3 = `${value.division} / ${value.branch} / ${value.department}`;
if (map.get(id3) != value) map.delete(id3);
const id2 = `${value.division} / ${value.branch} / ${undefined}`;
if (map.get(id2) != value) map.delete(id2);
const id1 = `${value.division} / ${undefined} / ${undefined}`;
if (map.get(id1) != value) map.delete(id1);
}
const result = Array.from(map.values());
console.log(result)

我假设最详细的是指包含最多属性的对象,因此是最详细的对象?

在这种情况下,您可以计算对象中的键的数量,并且只抓取键最多的对象,从而抓取最详细的对象。

编辑:如果对象总是有至少设置division键值,您可以执行以下步骤:

  1. 首先查找与division共享相同键值的对象。如果没有匹配,它是唯一的和最详细的。
  2. 提取对象中键/值对最多的对象。所以{ division: "val1", key2: "val2"}被返回,而{ division: "val1" }没有。

您应该最终获得具有唯一划分值的对象,并且是最详细的对象。

相关内容

  • 没有找到相关文章

最新更新