[英]I have a function that returns an Object's key, however I need to access the original Object from this value: Javascript
[英]javascript: I need to get an object key from the function argument
vm.contributorAmountPerYear [index-1]給我一個對象,我希望它的鍵成為函數的Year參數。
function getAgriAmount(year,amount,index) {
if (typeof amount !== "number" ) {
amount = parseInt(amount ||0);
};
var argiYearlyLocalCost = vm.argiterraYearlyLocalCost;
console.log(vm.contributorAmountPerYear[index-1].year);
}
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.