如何将循环输入的结果保存到对象中?

How can the result of the input from the cycle be saved to the object?

如何更改循环,输入将保存在这样的对象中:

expenses: {
    “Item name[1]” : “Item price[1]”,
    “Item name[2]” : “Item price[2]”
}

代码如下:

let appData = {
    expenses: {},
    value: 0,
    askExpenses: {},
    asking: function() {
        for (let i=0; i < 2; i++) {
            appData.askExpenses[i] = prompt('item name');
            appData.value[i] = +prompt('item value');
            }
    }
};
appData.asking();

您可以通过将变量括在方括号中来将标题用作键。

let appData = {
  expenses: {},
  value: 0,
  askExpenses: {},
  asking: function() {
    for (let i = 0; i < 2; i++) {
      title = prompt('item name');
      value = +prompt('item value');
      appData.expenses[[title]] = value;
    }
  }
};
appData.asking();

console.log(appData.expenses);