العودة إلى الدورة

Object Utility Methods: keys(), values(), and entries()

JavaScript: الدورة الكاملة للمبتدئين من 'الصفر إلى الاحتراف'

69. Object Utility Methods

Modern JavaScript provides static methods on the built-in Object constructor to help iterate and extract data from objects in array form, which is often necessary for using array iteration methods (map, filter).

javascript const user = { id: 101, role: 'guest', status: 'online' };

1. Object.keys(obj)

Returns an array containing all property names (keys) of the object.

javascript const keys = Object.keys(user); console.log(keys); // ['id', 'role', 'status']

2. Object.values(obj)

Returns an array containing all property values of the object.

javascript const values = Object.values(user); console.log(values); // [101, 'guest', 'online']

3. Object.entries(obj)

Returns an array of [key, value] pairs.

javascript const entries = Object.entries(user); /* Output: [ ['id', 101], ['role', 'guest'], ['status', 'online'] ] */

Benefit: Once you have the data in an array format (keys, values, or entries), you can use powerful array methods like .forEach(), .map(), and .filter().