我在对象内有一个空数组,如下所示,
const account = {
name: "David Reallycool",
expenses: []
}
我需要创建一个函数将费用添加到空数组中,我需要的结果是,
const account = {
name: "David Reallycool",
expenses: [
{
descrition: "Rent",
amount: 1000
},
{
description: "Coffee",
amount: 2.50
}
]
我该如何操纵它?
最佳答案
const addExpense = (expense) => {
account.expenses.push(expense)
}
// use like this
addExpense({ description: 'Rent', amount: 1000 })
addExpense({ description: 'Coffee', amount: 2.5 })
关于javascript - JavaScript 中的对象操作数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52531158/