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

How can I filter through an array of objects to select a value that does not have a slack in the output

I am trying to loop through an array of objects and want to select anything that has name: item but not name: item/anotheritem.

Example:

const orgUnitArray = [(organizationConfig.organizationalUnits)]

console.log(orgUnitArray)

console.log
  [
    [
      { name: 'Security', ignore: undefined },
      { name: 'Infrastructure', ignore: undefined },
      { name: 'Sandbox', ignore: undefined },
      { name: 'Workloads', ignore: undefined },
      { name: 'Workloads/Workload1', ignore: undefined },
      { name: 'Workloads/Workload2', ignore: undefined }
    ]
  ]

Expected:

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

console.log
  [
    [
      { name: 'Security', ignore: undefined },
      { name: 'Infrastructure', ignore: undefined },
      { name: 'Sandbox', ignore: undefined },
      { name: 'Workloads', ignore: undefined },
    ]
  ]

>Solution :

You can simply check whether a item’s name includes a "/" and filter() by that criteria.

const orgUnitArray = [
  {name: "Security", ignore: undefined},
  {name: "Infrastructure", ignore: undefined},
  {name: "Sandbox", ignore: undefined},
  {name: "Workloads", ignore: undefined},
  {name: "Workloads/Workload1", ignore: undefined},
  {name: "Workloads/Workload2", ignore: undefined}
];

console.log(orgUnitArray.filter(item => !item.name.includes("/")));
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