Hi Friends đź‘‹,

Welcome to Infinitbility ❤️!

Today, I’m going to show you how to convert object keys to array in typescript, here we use some javascript Objects method like Object.keys() method.

Sometime’s we need to create array from object keys and that’s why we simple method Object.keys()

interface User {
  id: number;
  name: string;
  domain?: string;
  age?: number;
  isActive?: boolean;
}

let user: User = {
  id: 1,
  name: "infinitbility",
  domain: "infinitbility.com",
  age: 2,
  isActive: 1
};

let userKeys = [];
const resultArray = Object.keys(user).map(key => userKeys.push(key));

When you run above code you will get all your objects property in array, check below output image.

Output

TypeScript, convert object keys to array example
TypeScript, convert object keys to array example

All the best đź‘Ť.