Which method would you use to retrieve an array containing the keys of an object let obj = {name: 'Alice', age: 25};? A) Object.entries(obj) B) Object.values(obj) C) Object.getOwnP... Which method would you use to retrieve an array containing the keys of an object let obj = {name: 'Alice', age: 25};? A) Object.entries(obj) B) Object.values(obj) C) Object.getOwnPropertyNames(obj) D) Object.keys(obj)
Understand the Problem
The question is asking which JavaScript method can be used to retrieve an array of keys from an object called 'obj' that contains properties for name and age. It presents four options, and the solution lies in identifying the correct method for obtaining the keys from the object.
Answer
Object.keys(obj)
The final answer is Object.keys(obj).
Answer for screen readers
The final answer is Object.keys(obj).
More Information
The Object.keys()
method retrieves the keys of an object's own enumerable properties, returning them as an array of strings. It is useful when you need to work with the keys of the object.
Tips
A common mistake is to confuse Object.keys() with Object.entries() or Object.values(), which return different data structures related to the object's entries or values, not its keys.
Sources
- Object.keys() - JavaScript - MDN Web Docs - Mozilla - developer.mozilla.org
- How to use the Object.keys, Object.values, and Object.entries ... - quora.com
AI-generated content may contain errors. Please verify critical information