I want to create pipe in Angular to reduce my object and return only certain key:value pairs based on the array of strings that represent the object.keys that I am sure will be present within the object. I have tried to implement my solution with the help of this thread Object index key type in Typescript but I manage to get my keys but their values are always undefined.
html file:
<ng-container *ngFor="let keyValue of asset | filterProperties: arrayOfObjectKeys ; let i = index">
<div class="details-col">
<span class="key">
{{ keyValue.value}} :
</span>
<span class="value">
{{ keyValue.key }}
</span>
</div>
</ng-container>
ts file:
arrayOfObjectKeys = ['name', 'quantity', 'code']
pipe:
import { Pipe, PipeTransform } from '@angular/core'
@Pipe({
name: 'filterProperties',
standalone: true,
})
export class FilterPropertiesPipe implements PipeTransform {
transform(object: Record<string, any>, keys: string[]) {
const omit = <T extends {}, K extends keyof T>(obj: T, ...keys: K[]) => {
Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key as K))) as Omit<T, K>
}
console.log('OBJECT', omit(object))
return omit(object)
}
}
just one precision: MyObject can be multiple types and even complex deep objects but I am sure that arrayOfObjectKeys will always have values that are equal to objectKeys of the passed object.
Thank you!
omit?