I am seeking a way to flatten a nested array containing different objects and retrieve only the existing values from specific property names within those objects.
Specifically, I am interested in extracting all the propertyId values from a nested array consisting of two distinct object types.
export interface LeftMenuItem {
text: string;
routerUrl?: string;
isExpanded: boolean;
propertyId?: string;
children: LeftMenuChildrenItem[];
}
export interface LeftMenuChildrenItem {
text: string;
routerUrl?. string;
propertyId?: string;
isCustomer: boolean
}
const leftMenuPropertyIds: string[] = [];
this.leftMenuItems.forEach(val1 => {
if (val1.propertyId) {
leftMenuPropertyIds.push(val1.propertyId);
}
if (val1.children.length > 0) {
val1.children.forEach(val2 => {
if (val2.propertyId) {
leftMenuPropertyIds.push(val2.propertyId);
}
});
}
});
console.log(leftMenuPropertyIds);