Console.log返回:
KEY1
未定义
我希望看到:
KEY1
["首先","第二"]
var testfunc = function(a) {
var matrix = {
key1: ["first", "second"],
key2: ["third", "fourth"]
};
var b = matrix.a;
console.log(a);
console.log(b);
}
var otherfunc = function() {
return "key1";
}
testfunc(otherfunc());
答案 0 :(得分:2)
您必须使用bracket notation来访问动态媒体资源。
所以它应该是:matrix[a]
而不是未定义的matrix.a
。
var testfunc = function(a) {
var matrix = {
key1: ["first", "second"],
key2: ["third", "fourth"]
};
var b = matrix[a];
console.log(a);
console.log(b);
}
var otherfunc = function() {
return "key1";
}
testfunc(otherfunc());

const obj = {
x: 'x',
y: 'y'
};
const x = 'y';
console.log(obj.x);
console.log(obj[x]);