2.8 KiB
2.8 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244c8 | ブラケット記法によるオブジェクトのプロパティへのアクセス | 1 | https://scrimba.com/c/cBvmEHP | 16163 | accessing-object-properties-with-bracket-notation |
--description--
オブジェクトのプロパティにアクセスする 2 つ目の方法は、ブラケット記法 ([]
) です。 アクセスしようとしているオブジェクトのプロパティの名前にスペースが含まれている場合は、ブラケット記法を使用する必要があります。
ただし、オブジェクトのプロパティにスペースが含まれていない場合もブラケット記法を使用できます。
次は、ブラケット記法を使用してオブジェクトのプロパティを読み取る例です。
const myObj = {
"Space Name": "Kirk",
"More Space": "Spock",
"NoSpace": "USS Enterprise"
};
myObj["Space Name"];
myObj['More Space'];
myObj["NoSpace"];
myObj["Space Name"]
は文字列 Kirk
、myObj['More Space']
は文字列 Spock
、myObj["NoSpace"]
は文字列 USS Enterprise
となります。
スペースを含むプロパティ名には引用符 (シングルクォートまたはダブルクォート) が必要なことに注意してください。
--instructions--
ブラケット記法を使用して、testObj
のプロパティである an entree
と the drink
の値を読み取り、それぞれ entreeValue
と drinkValue
に割り当ててください。
--hints--
entreeValue
は文字列である必要があります。
assert(typeof entreeValue === 'string');
entreeValue
の値は文字列 hamburger
である必要があります。
assert(entreeValue === 'hamburger');
drinkValue
は文字列である必要があります。
assert(typeof drinkValue === 'string');
drinkValue
の値は文字列 water
である必要があります。
assert(drinkValue === 'water');
ブラケット記法を 2 回使用する必要があります。
assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
--seed--
--after-user-code--
(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
--seed-contents--
// Setup
const testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
// Only change code below this line
const entreeValue = testObj; // Change this line
const drinkValue = testObj; // Change this line
--solutions--
const testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
const entreeValue = testObj["an entree"];
const drinkValue = testObj['the drink'];