javascript:我需要从函数参数中获取一个对象键
vm.contributorAmountPerYear[index-1]获取一个对象,我希望它的键是函数的year参数javascript:我需要从函数参数中获取一个对象键,javascript,function,object,Javascript,Function,Object,vm.contributorAmountPerYear[index-1]获取一个对象,我希望它的键是函数的year参数 function getAgriAmount(year,amount,index) { if (typeof amount !== "number" ) { amount = parseInt(amount ||0); }; var argiYearlyLocalCost = vm.ar
function getAgriAmount(year,amount,index) {
if (typeof amount !== "number" ) {
amount = parseInt(amount ||0);
};
var argiYearlyLocalCost = vm.argiterraYearlyLocalCost;
console.log(vm.contributorAmountPerYear[index-1].year);
}
对于任何javascript对象,都应该记住,如果使用
点表示法,则无法访问来自变量且在运行时确定的键的属性。在这种情况下使用方括号符号[]。这应该起作用:
vm.contributorAmountPerYear[index-1][year];
当您已经知道密钥时,应使用点符号:
var cuteJavaScriptObject = {
animal : 'cat'
}
var myVar = 'animal';
console.log(cuteJavaScriptObject.animal); // OK
console.log(cuteJavaScriptObject.myVar); // Wrong !!
console.log(cuteJavaScriptObject[myVar]); // Now OK
var cuteJavaScriptObject = {
animal : 'cat'
}
var myVar = 'animal';
console.log(cuteJavaScriptObject.animal); // OK
console.log(cuteJavaScriptObject.myVar); // Wrong !!
console.log(cuteJavaScriptObject[myVar]); // Now OK