我有两个数组。我正在使用 PubSidebar 过滤基于 groupKey。
let groupKey = ['oaDeal', 'Journals', 'Deposit']
// This array of object will be filtering with groupKey
const PubSidebar = [
{
value: 'Dashboard',
role: 'public',
},
{
value: 'oaDeal',
role: 'private',
content: [
{
role: 'private',
value: 'oaDeal',
},
],
},
{
value: 'Journals',
role: 'public',
content: [
{
role: 'private',
value: 'Journals',
},
{
role: 'private',
value: 'Token',
},
{
role: 'private',
value: 'policy',
},
{
role: 'private',
value: 'Deposit',
},
{ role: 'public', value: 'test' },
],
},
]
// Here is my trying code. I am filtering PubSidebar
const res = PubSidebar.filter(x => {
// when it's main outerloop role public or private and groupKey matched
if (
x.role === 'public' ||
(x.role === 'private' && groupKey.includes(x.value))
) {
// then if inner-array exist then inner array filtering
if (x.content) {
// inside content assign condition public or private and groupKey
let tempX = x.content.filter(
y =>
y.role === 'public' ||
(y.role === 'private' && groupKey.includes(y.value)) ||
x.value === y.value
)
x.content = tempX
console.log(tempX)
return x
} else {
// Other wise give me a single object public
console.log(x)
return x
}
}
})
如果父值:期刊或存款或任何值或角色:公共,我将面临在内容数组中传递对象的问题。我必须基于groupKey
.
如果 Journals 和 Deposits 存在,则在 content 数组中添加 Journals 和 Deposit 数据,包括公共数据。(三个对象)
如果 Journals 存在,则在包含公共数据(两个对象)的内容数组中添加 Journals 数据如果存在 Deposits ,则在包含公共数据(两个对象)的内容数组中添加 Deposits 数据
如果 GroupKey journals 与 pubsidebar 中的内容对象匹配,则为两个对象,我们将得到
{
value: 'Journals',
role: 'public',
content: [
{
role: 'private',
value: 'Journals',
},
{ role: 'public', value: 'test' },
],
}
如果 GroupKey Deposits 与 pubsidebar 中的内容对象匹配,则有两个对象
{
value: 'Deposit',
role: 'public',
content: [
{
role: 'private',
value: 'Deposit',
},
{ role: 'public', value: 'test' },
],
}
如果 GroupKey Journals and Deposits 与 pubsidebar 中的内容对象匹配,则为三个对象,
{
value: 'Deposit' || "Journals",
role: 'public',
content: [
{
role: 'private',
value: 'Journals',
},
{
role: 'private',
value: 'Deposit',
},
{ role: 'public', value: 'test' },
],
}