Open In App

TypeScript Array Object.values() Method

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Object.values() is a functionality in TypeScript that returns an array extracting the values of an object. The order of elements is the same as they are in the object.

Syntax:

Object.values(object);

Parameters:

  • object: Here you have to pass the object whose values you want to extract.

Return Value:

An array that contains the property values of the given object which are enumerable and have string keys.

NOTE: Since Object.values() is not directly available in TypeScript, we will implement it using the interface class.

Example 1: The below code example is the bais implementation of object.values() method in TypeScript.

Javascript




interface Geek {
  name: string;
  workForce: number;
}
 
const geek: Geek = {
  name: "GeeksforGeeks",
  workForce: 200
};
 
const valArray: (number | string)[] =
Object.values(geek)
 
console.log(valArray);


Output:

["GeeksforGeeks", 200]

Example 2: Below is another implementation of the Object.values() method.

Javascript




interface Car {
    carName: string;
    carNumber: string;
    model: number,
    sportsCar: boolean
}
 
const car1: Car = {
    carName: "Alto",
    carNumber: "AB05XXXX",
    model: 2023,
    sportsCar: false
};
 
const car2: Car = {
    carName: "F1",
    carNumber: "AB05X0X0",
    model: 2024,
    sportsCar: true
};
 
const carArr1 = Object.values(car1);
const carArr2 = Object.values(car2);
 
console.log(carArr1);
console.log(carArr2);


Output:

["Alto", "AB05XXXX", 2023, false]
["F1", "AB05X0X0", 2024, true]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads