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!

Marco
  • 1,051
  • 1
  • 17
  • 40
  • Assignment in JS uses `=` and `.filter` filters arrays.https://developer.mozilla.org/en-us/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – evolutionxbox Feb 04 '21 at 11:24
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – evolutionxbox Feb 04 '21 at 11:25
  • I've changed my code. It kind of answer my question but the issue is that i cannot access permission – Marco Feb 04 '21 at 13:24

2 Answers2

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)
Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
  • (this question is likely a duplicate of https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – evolutionxbox Feb 04 '21 at 11:25
  • i modified my array... i've tried your suggestion but it just store the label home and not the child – Marco Feb 04 '21 at 11:49
  • 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 – Satyam Pathak Feb 05 '21 at 03:50
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);
   
Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31