Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS filter an array object

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

json_menu.menu.filter(record => record.active === 'c1')

but i obtain an empty object.

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading