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.
1interface User {2 id: number;3 name: string;4 domain?: string;5 age?: number;6 isActive?: boolean;7}89let user: User = {10 id: 1,11 name: "infinitbility",12 domain: "infinitbility.com",13 age: 2,14 isActive: 115};1617let userDetail = [];18const 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.
Output

All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.