'how to filter role from children of array based on other array in javascript
I need to filter role from children of array based on other array, but i'm still confused on using filter in my case
this is the base array :
arr1 = [
{
name: 'test1',
children: [
{
name: 'one',
role: 'menu_one'
},
{
name: 'two',
role: 'menu_two'
},
{
name: 'three',
role: 'menu_three'
}
]
},
{
name: 'test2',
children: [
{
name: 'four',
role: 'menu_four'
},
{
name: 'five',
role: 'menu_five'
}
]
}
]
this is accepted role :
arr2 = ['menu_one', 'menu_three', 'menu_four']
this is what i'am trying to accomplish:
arr1 = [
{
name: 'test1',
children: [
{
name: 'one',
role: 'menu_one'
},
{
name: 'three',
role: 'menu_three'
}
]
},
{
name: 'test2',
children: [
{
name: 'four',
role: 'menu_four'
}
]
}
]
I try this and is not work, did i use it wrong?
var result = arr1.filter(function(item) {
return arr2.includes(item.children.role);
})
I hope someone can help my case
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
