Files
2022-01-20 20:30:18 +01:00

2.2 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b7d367417b2b2512b1e Object.keys() による全オブジェクトキーの配列の生成 1 301160 generate-an-array-of-all-object-keys-with-object-keys

--description--

Object.keys()メソッドを使用し、オブジェクトを引数として渡すことで、オブジェクトにあるすべてのキーを含む配列を生成することも可能です。 この場合は、オブジェクト内の各プロパティを表す文字列を持つ配列を返します。 ここでも、配列内のエントリに対する特定の順序はありません。

--instructions--

getArrayOfUsers 関数の記述を完成させて、引数として受け取ったオブジェクトのすべてのプロパティを含む配列を返してください。

--hints--

users オブジェクトには AlanJeffSarahRyan のキーのみを含めます。

assert(
  'Alan' in users &&
    'Jeff' in users &&
    'Sarah' in users &&
    'Ryan' in users &&
    Object.keys(users).length === 4
);

getArrayOfUsers 関数は、users オブジェクト内のすべてのキーを含む配列を返す必要があります。

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--

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--

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));