Keys from Variables in JavaScript Multidimensional Structures
Let the following object be given:
let obj = {
'sub1': ['11', '12', '13'],
'sub2': ['21', '22', '23'],
};
Let's output some element from our
object, for example, the element '22':
console.log(obj['sub2'][1]);
Now let the keys be stored in variables:
let key1 = 'sub2';
let key2 = 1;
Let's output the element of the multidimensional structure using our variables:
console.log(obj[key1][key2]);
Given the following structure for storing a list of affairs by years, months, and days:
let affairs = {
'2018': {
11: {
29: ['name111', 'name112', 'name113'],
30: ['name121', 'name122', 'name123'],
},
12: {
30: ['name211', 'name212', 'name213'],
31: ['name221', 'name222', 'name223'],
},
},
'2019': {
12: {
29: ['name311', 'name312', 'name313'],
30: ['name321', 'name322', 'name323'],
31: ['name331', 'name332', 'name333'],
}
},
}
Also, let three variables be given, containing the year, month, and day. Output the affair corresponding to the values of the variables.
The author of the following code wanted to output the element
with the value '24':
let obj = {
key1: {
key2: '12',
key3: '13',
},
key2: {
key4: '24',
key5: '25',
},
}
let key1 = 'key2';
let key2 = 'key4';
console.log(obj['key1'][key2]);
However, the code does not output what the author expected. Correct the mistake.
The author of the following code wanted to output the element
with the value '24':
let obj = {
key1: {
key2: '12',
key3: '13',
},
key2: {
key4: '24',
key5: '25',
},
}
let key1 = 'key2';
let key2 = 'key4';
console.log(obj.key1.key2);
However, the code does not output what the author expected. Correct the mistake.
The author of the following code wanted to output the element
with the value '24':
let obj = {
key1: {
key2: '12',
key3: '13',
},
key2: {
key4: '24',
key5: '25',
},
}
let key1 = 'key2';
let key2 = 'key4';
console.log(obj.key1['key2']);
However, the code does not output what the author expected. Correct the mistake.
The author of the following code wanted to output the element
with the value '24':
let obj = {
key1: {
key2: '12',
key3: '13',
},
key2: {
key4: '24',
key5: '25',
},
}
let key1 = 'key2';
console.log(obj['key1']['key4']);
However, the code does not output what the author expected. Correct the mistake.