2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b7d367417b2b2512b1e
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:21 +08:00
|
|
|
forumTopicId: 301160
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 使用 Object.Keys() 生成对象所有键组成的数组
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:14:21 +08:00
|
|
|
<section id='description'>
|
|
|
|
我们还可以输入一个对象作为参数来调用<code>Object.keys()</code>方法,使其生成一个包含对象中所有键的数组。这会返回一个由对象中所有键的名称(字符串)组成的数组。再次说明,这个数组中的项的顺序是不确定的。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:21 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
请你完成<code>getArrayOfUsers</code>函数,使其返回一个包含输入的对象的所有属性的数组。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-08-04 15:14:21 +08:00
|
|
|
- text: <code>users</code>对象应该只包含<code>Alan</code>、<code>Jeff</code>、<code>Sarah</code>和<code>Ryan</code>这 4 个键
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert('Alan' in users && 'Jeff' in users && 'Sarah' in users && 'Ryan' in users && Object.keys(users).length === 4);
|
2020-08-04 15:14:21 +08:00
|
|
|
- text: <code>getArrayOfUsers</code>函数应该返回一个包含<code>users</code>对象中所有键的数组
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: 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);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```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) {
|
|
|
|
// change code below this line
|
|
|
|
|
|
|
|
// change code above this line
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(getArrayOfUsers(users));
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:21 +08:00
|
|
|
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));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:14:21 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|