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:
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("/")));