我想使用给定的记录创建数组的数组。
user_permission =
[ { _id: 1,
description: 'chairman list',
main_module: 'admin',
sub_module: 'admin' },
{ _id: 2,
description: 'create chairman get method',
main_module: 'admin',
sub_module: 'admin' },
{ _id: 3,
description: 'create chairman post method',
main_module: 'admin',
sub_module: 'admin' },
{ _id: 4,
description: 'chairman list post',
main_module: 'chairman',
sub_module: 'chairman' },
{ _id: 5,
description: 'vendor list',
main_module: 'vendor',
sub_module: 'vendor' },
{ _id: 6,
description: 'vendor list post',
main_module: 'vendor',
sub_module: 'vendor' } ]
使用此数组,我想像给定示例一样生成这种类型的数组
[
[ 'admin > admin': { '1': 'chairman list' },
{ '2': 'create chairman get method' },
{ '3': 'create chairman post method' }
],
[ 'chairman > chairman': { '4': 'chairman list post' }
],
[ 'vendor > vendor': { '5': 'vendor list' },
{ '6': 'vendor list post' }
]
]
使用foreach并获取结果,但是我没有创建适当的数组
user_permission.forEach(function(data){
var mainmodule = [[data.main_module]+" > "+[data.sub_module]];
var key = data._id;
var val = data.description;
var elem = new Array();
elem[mainmodule] ={};
elem[mainmodule][key] = val;
user_prems.push(elem);
})
foreach的结果如下:-
[
[ 'admin > admin': { '1': 'chairman list' } ],
[ 'admin > admin': { '2': 'create chairman get method' } ],
[ 'admin > admin': { '3': 'create chairman post method' } ],
[ 'chairman > chairman': { '4': 'chairman list post' } ],
[ 'vendor > vendor': { '5': 'vendor list' } ],
[ 'vendor > vendor': { '6': 'vendor list post' } ]
]
答案 0 :(得分:4)
首先,您的预期输出不正确。您应该有一个对象而不是数组。并且他们使用reduce()
来获得一个对象,该对象的键为admin > admin,...
,而值作为数组的对象将包含对象
const user_permission = [ { _id: 1, description: 'chairman list', main_module: 'admin', sub_module: 'admin' }, { _id: 2, description: 'create chairman get method', main_module: 'admin', sub_module: 'admin' }, { _id: 3, description: 'create chairman post method', main_module: 'admin', sub_module: 'admin' }, { _id: 4, description: 'chairman list post', main_module: 'chairman', sub_module: 'chairman' }, { _id: 5, description: 'vendor list', main_module: 'vendor', sub_module: 'vendor' }, { _id: 6, description: 'vendor list post', main_module: 'vendor', sub_module: 'vendor' } ]
const res = user_permission.reduce((ac,a) => {
let key = `${a.main_module} > ${a.sub_module}`
if(!ac[key]) ac[key] = [];
ac[key].push({[a._id]:a.description})
return ac;
},{})
console.log(res)
答案 1 :(得分:3)
您可以使用reduce
来转换数据:
let user_permission = [
{ _id: 1, description: 'chairman list', main_module: 'admin', sub_module: 'admin' },
{ _id: 2, description: 'create chairman get method', main_module: 'admin', sub_module: 'admin' },
{ _id: 3, description: 'create chairman post method', main_module: 'admin', sub_module: 'admin' },
{ _id: 4, description: 'chairman list post', main_module: 'chairman', sub_module: 'chairman' },
{ _id: 5, description: 'vendor list', main_module: 'vendor', sub_module: 'vendor' },
{ _id: 6, description: 'vendor list post', main_module: 'vendor', sub_module: 'vendor' } ]
let result = user_permission.reduce((prev, current) => {
let key = `${current.main_module} > ${current.sub_module}`;
let group = prev.find(x => x[key]);
if(!group){
group = { [key]: [] };
prev.push(group);
}
group[key].push({ [current._id]: current.description });
return prev;
}, [])
console.log(result);