0

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!

3

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

3 Comments

(this question is likely a duplicate of stackoverflow.com/questions/2722159/…)
i modified my array... i've tried your suggestion but it just store the label home and not the child
HI @Marco I just showed you the way to achieve what you are expecting, Now you need to go that path by yourself :) Happy to help
0

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);
   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.