I have got two arrays . I am filtering based groupKey with PubSidebar.
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
}
}
})
I am facing problem to pass objects inside content array if parents value: Journals or Deposits or any value or role:pubic. I have to pass value inside content array based on groupKey.
If Journals and Deposits is existed , then adding Journals and Deposit data inside content array, including with public data . (three Objects)
If Journals is existed , then adding Journals data inside content array including with public data(two Objects) If Deposits is existed , then adding Deposits data inside content array including with public data(two Objects)
if GroupKey journals is matched with content object in pubsidebar then two objects , we will get
{
value: 'Journals',
role: 'public',
content: [
{
role: 'private',
value: 'Journals',
},
{ role: 'public', value: 'test' },
],
}
if GroupKey Deposits is matched with content object in pubsidebar then two objects
{
value: 'Deposit',
role: 'public',
content: [
{
role: 'private',
value: 'Deposit',
},
{ role: 'public', value: 'test' },
],
}
if GroupKey Journals and Deposits is matched with content object in pubsidebar then three objects ,
{
value: 'Deposit' || "Journals",
role: 'public',
content: [
{
role: 'private',
value: 'Journals',
},
{
role: 'private',
value: 'Deposit',
},
{ role: 'public', value: 'test' },
],
}