Create an array from the keys of the object obj below, with all of the keys converted to uppercase. Your implementation must not mutate obj.
let obj = {
b: 2,
a: 1,
c: 3,
};
The order of the array does not matter.
When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more
Create an array from the keys of the object obj below, with all of the keys converted to uppercase. Your implementation must not mutate obj.
let obj = {
b: 2,
a: 1,
c: 3,
};
The order of the array does not matter.
let objKeys = Object.keys(obj);
let upperKeys = objKeys.map((key) => key.toUpperCase());
console.log(upperKeys); // => [ 'B', 'A', 'C' ]
console.log(obj); // => { b: 2, a: 1, c: 3 }
The challenge of this exercise is to figure out how to iterate through the properties of obj. We showed two ways in this chapter: for/in with hasOwnProperty() and Object.keys(). The former involves a bit more work, so we use Object.keys() combined with map() to extract and uppercase the keys into an array.
We can also use forEach, though it requires a bit more effort:
let upperKeys = [];
let objKeys = Object.keys(obj);
objKeys.forEach(function(key) {
upperKeys.push(key.toUpperCase());
});
console.log(upperKeys); // => [ 'B', 'A', 'C' ]
Video Walkthrough
Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .
Submit your solution for LSBot review. Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .