Hi Friends 👋,

Welcome to Infinitbility ❤️!

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

Sometime’s we need to create array from object we have to iterate a object.

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 userDetail = [];
const resultArray = Object.keys(user).map(key => userDetail.push([key, user[key]]));

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


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

All the best 👍.