I have the following JSON:
"menu":[
{
"name": "Home",
"icon": "ri-home-4-line",
"layoutCode": "",
"active": ["h1"]
},
{
"name": "Cust",
"layoutCode": "",
"icon": "ri-user-2-fill",
"active": ["c1", "c2"]
}
]
What I would like to do is filter by active and get the parent object.
I tried with this:
json_menu.menu.filter(record => record.active === 'c1')
but i obtain an empty object.
CodePudding user response:
You need to check if active
includes "c1"
.
const menu = [
{
name: "Home",
icon: "ri-home-4-line",
layoutCode: "",
active: ["h1"]
},
{
name: "Cust",
layoutCode: "",
icon: "ri-user-2-fill",
active: ["c1", "c2"]
}
];
const result = menu.filter(m => m.active.includes("c1"));
console.log(result);
CodePudding user response:
active
is an array. Try:
json_menu.menu.filter(record => record.active.includes('c1'))
CodePudding user response:
You can use includes
instead of ===
json_menu.menu.filter(record => record.active.includes('c1'))
CodePudding user response:
json_menu.menu.filter(record => record.active.includes('c1'))
use includes function to check item in array