我有两个数组。我正在使用 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 存在,则将 Journals 和 Deposits 数据添加到 content 数组中,包括公共数据。 (三个对象)
如果 Journals 存在,则将 Journals 数据添加到内容数组中,包括公共数据(两个对象)
如果存款存在,则将存款数据添加到内容数组中,包括公共数据(两个对象)
如果 GroupKey 期刊与 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 和 Deposits 与 pubsidebar 中的内容对象匹配,则三个对象,
{
value: 'Deposit' || "Journals",
role: 'public',
content: [
{
role: 'private',
value: 'Journals',
},
{
role: 'private',
value: 'Deposit',
},
{ role: 'public', value: 'test' },
],
}