Marco
Marco

Reputation: 1061

React Filter array of object

I Have an array of object like this:

 export const SITE_MAP = [
{
 label: "Home",
 path: "/homepage",
 expansion: [],
},
{
label: "user",
expansion: [
  {
    label: "Catalogue",
    icon: "account",
    path: "pat_to",
    permission: "yes",
    items: []

I need to create a new array myHome which contains all the objects with permission yes. How can i filter the SITE_MAP and assign it to the new array Myhome?

Thank you!

Upvotes: 0

Views: 359

Answers (2)

Yves Kipondo
Yves Kipondo

Reputation: 5623

You can use Array.prototype.filter method to filter retrieve only Object which you need.

const SITE_MAP = [
  {
      label: "Schemes",
      icon: "control_camera",
      path: "control",
      permission: "yes",
      items: []
   }
]

const myHome = SITE_MAP.filter(site =>{
  return site.permission === "yes";
});

console.log(myHome);
   

Upvotes: 0

Satyam Pathak
Satyam Pathak

Reputation: 6932

const SITE_MAP = [{
    label: "Schemes",
    icon: "control_camera",
    path: "control",
    permission: "yes",
    items: []
 },{
    label: "Schemes",
    icon: "control_camera",
    path: "control",
    permission: "no",
    items: []
  }
 ]
 
 const myHome = SITE_MAP.filter(el => el.permission === 'yes')
 
 console.log(myHome)

Upvotes: 2

Related Questions