0

我正在尝试通过在 objectProperty 的末尾添加一个变量以选择属性来 console.log out 一个 true/false 语句:(appleAmount.amount+number)?你怎么做呢?

let apple = 48;
let appleAmount = {
  amount0 : 20,
  amount1 : 40,
  amount2 : 48
}

let number = 2;
console.log(apple === appleAmount.amount+number);
4

1 回答 1

1

在 JavaScript 中,您可以通过字符串名称访问属性,而不仅仅是通过点运算符:

console.log(apple === appleAmount[`amount${number}`]); // If your version supports interpolation
console.log(apple === appleAmount['amount' + number]);
于 2017-07-10T22:30:15.663 回答