mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-12 01:00:13 -04:00
* fix(curriculum): increase readability of the introduction * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/generate-an-array-of-all-object-keys-with-object.keys.md Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org>
112 lines
2.1 KiB
Markdown
112 lines
2.1 KiB
Markdown
---
|
|
id: 587d7b7d367417b2b2512b1e
|
|
title: Generate an Array of All Object Keys with Object.keys()
|
|
challengeType: 1
|
|
forumTopicId: 301160
|
|
dashedName: generate-an-array-of-all-object-keys-with-object-keys
|
|
---
|
|
|
|
# --description--
|
|
|
|
We can also generate an array which contains all the keys stored in an object with the `Object.keys()` method. This method takes an object as the argument and returns an array of strings representing each property in the object. Again, there will be no specific order to the entries in the array.
|
|
|
|
# --instructions--
|
|
|
|
Finish writing the `getArrayOfUsers` function so that it returns an array containing all the properties in the object it receives as an argument.
|
|
|
|
# --hints--
|
|
|
|
The `users` object should only contain the keys `Alan`, `Jeff`, `Sarah`, and `Ryan`
|
|
|
|
```js
|
|
assert(
|
|
'Alan' in users &&
|
|
'Jeff' in users &&
|
|
'Sarah' in users &&
|
|
'Ryan' in users &&
|
|
Object.keys(users).length === 4
|
|
);
|
|
```
|
|
|
|
The `getArrayOfUsers` function should return an array which contains all the keys in the `users` object
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
users.Sam = {};
|
|
users.Lewis = {};
|
|
let R = getArrayOfUsers(users);
|
|
return (
|
|
R.indexOf('Alan') !== -1 &&
|
|
R.indexOf('Jeff') !== -1 &&
|
|
R.indexOf('Sarah') !== -1 &&
|
|
R.indexOf('Ryan') !== -1 &&
|
|
R.indexOf('Sam') !== -1 &&
|
|
R.indexOf('Lewis') !== -1
|
|
);
|
|
})() === true
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
let users = {
|
|
Alan: {
|
|
age: 27,
|
|
online: false
|
|
},
|
|
Jeff: {
|
|
age: 32,
|
|
online: true
|
|
},
|
|
Sarah: {
|
|
age: 48,
|
|
online: false
|
|
},
|
|
Ryan: {
|
|
age: 19,
|
|
online: true
|
|
}
|
|
};
|
|
|
|
function getArrayOfUsers(obj) {
|
|
// Only change code below this line
|
|
|
|
// Only change code above this line
|
|
}
|
|
|
|
console.log(getArrayOfUsers(users));
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
let users = {
|
|
Alan: {
|
|
age: 27,
|
|
online: false
|
|
},
|
|
Jeff: {
|
|
age: 32,
|
|
online: true
|
|
},
|
|
Sarah: {
|
|
age: 48,
|
|
online: false
|
|
},
|
|
Ryan: {
|
|
age: 19,
|
|
online: true
|
|
}
|
|
};
|
|
|
|
function getArrayOfUsers(obj) {
|
|
return Object.keys(obj);
|
|
}
|
|
|
|
console.log(getArrayOfUsers(users));
|
|
```
|