0

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!

2
  • Aren't you missing an explicit return in omit ? Commented Feb 5, 2024 at 16:03
  • no even if I add this it is not working Commented Feb 5, 2024 at 16:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.