0

I have a problem, that I need to filter all the object present inside the array of objects on basis of keys and need to transform these array of objects into another type of objects

I have tried in this way ...

const values = Object.keys(user).map((key) => {'refKey': key});
console.log(values);

but it's not working! Please help!

Sarun UK
  • 6,210
  • 7
  • 23
  • 48

1 Answers1

0

values will automatically take the type as array of values returned by map function. So for most of the cases you don't need to explicitly define the type.

For example, refer the below

let user: Record<string, string> = {
    name: 'Tony Stark',
    pet: 'Jarvis'
}

type X = {
    key: string,
    value: string
}

const values = Object.keys(user).map(([k, v]): X => ({'key': k, 'value': v}));
// values: Array<X>

In this your map function doesn't need to explicitly declare the type. Still the below also works

const values = Object.keys(user).map(([k, v]) => ({'key': k, 'value': v}));

In short, just declare the return type of your mapper function which will be considered.

Ram Babu
  • 2,692
  • 3
  • 23
  • 28