首先,我是Java,ES6等的新手,而且我来自Java。 我有一个复杂的javascript数组结构(下面给出了示例), 我正在尝试将此数组转换为映射(类似于Java的方式,键值对之类的东西),键为权限名称(例如KEY-1,KEY-2,KEY-3,KEY-4,KEY-关于下面的javascript数组示例,其值为5),而value是实际权限的逗号分隔值。我可以通过遍历嵌套数组来实现它,但是循环是我在这里要避免的事情,我想使用map()/ reduce()/ filter()
这里是地图应如何包含数据的示例。由于两个数组中都存在KEY-2,因此它们将被覆盖为一个(完全可以)
KEY-1 = ["Roles.Create","Roles.Edit"]
KEY-2 = ["API-Admin.Create","API-Admin.Edit","API-Admin.Read"]
KEY-3 = ["Roles.Create","Roles.Edit"]
KEY-4 = ["Users.Read"]
KEY-5 = ["Roles.Create","Roles.Edit"]
const teamArr = [
{
"name":"Team1",
"accountId":"Billing",
"teamId":"12345",
"permissions": {
"KEY-1": [
"Roles.Create",
"Roles.Edit"
],
"KEY-2": [
"API-Admin.Create",
"API-Admin.Edit",
"API-Admin.Read"
],
"KEY-3": [
"Roles.Create",
"Roles.Edit"
]
}
},
{
"name":"Team2",
"accountId":"Sales",
"teamId":"6789",
"permissions": {
"KEY-4": [
"Users.Read"
],
"KEY-2": [
"API-Admin.Create",
"API-Admin.Edit",
"API-Admin.Read"
],
"KEY-5": [
"Roles.Create",
"Roles.Edit"
]
}
}
]
KEY-1,KEY-2,KEY-3,KEY-4,KEY-5等都是动态生成的,因此我无法将这些键名称硬编码到我的代码中(例如KEY-1,KEY-2等)。 )
我关注了这篇帖子https://www.freecodecamp.org/news/15-useful-javascript-examples-of-map-reduce-and-filter-74cbbb5e0a1f/ 下面是我尝试过的方法,但是我想我正在努力在复杂的JavaScript数组上正确使用sort()/ reduce()。我更喜欢普通的Javascript / ES6解决方案(没有JQuery pls)。
const sorted = test.sort((a, b) => a.permissions - b.permissions);
// Using reduce:
dict = sorted.reduce(
(dict, el, index) => (dict[el.permissions] = sorted.length - index, dict),
{}
);
console.log(dict)
在这里的任何帮助将不胜感激。谢谢
答案 0 :(得分:2)
您可以结合使用flatMap
和reduce
来做到这一点(尽管尚不支持所有浏览器使用flatMap
):
const teamArr = [{
"name": "Team1",
"accountId": "Billing",
"teamId": "12345",
"permissions": {
"KEY-1": [
"Roles.Create",
"Roles.Edit"
],
"KEY-2": [
"API-Admin.Create",
"API-Admin.Edit",
"API-Admin.Read"
],
"KEY-3": [
"Roles.Create",
"Roles.Edit"
]
}
},
{
"name": "Team2",
"accountId": "Sales",
"teamId": "6789",
"permissions": {
"KEY-4": [
"Users.Read"
],
"KEY-2": [
"API-Admin.Create",
"API-Admin.Edit",
"API-Admin.Read"
],
"KEY-5": [
"Roles.Create",
"Roles.Edit"
]
}
}
]
const result = teamArr
.flatMap(t => Object.entries(t.permissions))
.reduce((acc, [key, permissions]) => {
acc[key] = acc[key] || [];
acc[key].push(...permissions.filter(p => !acc[key].includes(p)));
return acc;
}, {});
console.log(result);
如果某个密钥的权限数组可能很大,那么您也可以考虑在累加器中每个密钥旁边使用一个Set
。
答案 1 :(得分:1)
由于您不必担心其他键/值,因此需要首先创建所有permissions
对象的数组。然后,您从该数组中提取属性,并通过Sets保持数组唯一:
const teamArr = [{"name":"Team1","accountId":"Billing","teamId":"12345","permissions":{"KEY-1":["Roles.Create","Roles.Edit"],"KEY-2":["API-Admin.Create","API-Admin.Edit","API-Admin.Read"],"KEY-3":["Roles.Create","Roles.Edit"]}},{"name":"Team2","accountId":"Sales","teamId":"6789","permissions":{"KEY-4":["Users.Read"],"KEY-2":["API-Admin.Create","API-Admin.Edit","API-Admin.Read"],"KEY-5":["Roles.Create","Roles.Edit"]}}];
const res = teamArr.flatMap(({ permissions }) => Object.entries(permissions)).reduce((a, [k, v]) => ((a[k] = [...new Set(((a[k] = a[k] || []).push(...v), a[k]))], a)), {});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
更多详细版本:
const teamArr = [{"name":"Team1","accountId":"Billing","teamId":"12345","permissions":{"KEY-1":["Roles.Create","Roles.Edit"],"KEY-2":["API-Admin.Create","API-Admin.Edit","API-Admin.Read"],"KEY-3":["Roles.Create","Roles.Edit"]}},{"name":"Team2","accountId":"Sales","teamId":"6789","permissions":{"KEY-4":["Users.Read"],"KEY-2":["API-Admin.Create","API-Admin.Edit","API-Admin.Read"],"KEY-5":["Roles.Create","Roles.Edit"]}}];
const permissions = teamArr.map(({ permissions }) => permissions);
const res = permissions.flatMap(Object.entries).reduce((a, [k, v]) => {
a[k] = a[k] || [];
a[k].push(...v);
a[k] = [...new Set(a[k])];
return a;
}, {});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
答案 2 :(得分:0)
const teamArr = [
{
"name":"Team1",
"accountId":"Billing",
"teamId":"12345",
"permissions": {
"KEY-1": [
"Roles.Create",
"Roles.Edit"
],
"KEY-2": [
"API-Admin.Create",
"API-Admin.Edit",
"API-Admin.Read"
],
"KEY-3": [
"Roles.Create",
"Roles.Edit"
]
}
},
{
"name":"Team2",
"accountId":"Sales",
"teamId":"6789",
"permissions": {
"KEY-4": [
"Users.Read"
],
"KEY-2": [
"API-Admin.Create",
"API-Admin.Edit",
"API-Admin.Read"
],
"KEY-5": [
"Roles.Create",
"Roles.Edit"
]
}
}
];
function extractPermissions(obj) {
const perms = {};
obj.forEach(entry => {
if (!entry['permissions']) {
return;
}
Object.keys(entry.permissions).forEach(key => {
if (!perms[key]) {
perms[key] = [];
}
entry.permissions[key].forEach(value => {
if (!perms[key].some(val => val === value)) {
perms[key].push(value);
}
});
});
});
return perms;
}
console.log(JSON.stringify(extractPermissions(teamArr), null, 2));