[英]Javascript - Add new object into array of objects
请。 我有一个未婚夫余额的周期。 它是一个对象数组,例如:
export const balances = [
type: types.outgoing,
date: 20220410,
amount: 282.12,
category: categories.installments,
source: 'Debit account',
destination: 'Leasing company',
},
{
type: types.income,
date: 20220413,
amount: 1385.3,
category: categories.job,
source: 'My employeer',
destination: 'Debit account',
},
...
]
ETC...
正如您所看到的,我在那里有一个类别,这意味着我在循环中有balances
中的每笔交易,我必须为每个交易创建单独的类别,每个类别的总金额,类别中的项目计数以及每个类别的详细交易. 我正在使用array.forEach()
循环:
balances.forEach((balance) => {
// Checking if category already exists in my array of categories
let categoryIndex = categories.findIndex((category) => category.category === balance.category)
// Create details of transaction
let transactionDetail = {
date: balance.date,
amount: balance.amount,
source: balance.source,
destination: balance.destination,
}
// If category already exists, just calculate total and add new object into array details
if (categoryIndex !== -1) {
console.log([categories[categoryIndex].details])
categories[categoryIndex] = {
type: balance.type,
category: balance.category,
amount: categories[categoryIndex].amount + balance.amount,
count: (categories[categoryIndex].count += 1),
// This row is wrong. I cannot use this
details: [categories[categoryIndex].details].push(transactionDetail),
}
} else {
// If category doesn't yet exists, we must create a first values in this category
categories.push({
type: balance.type,
category: balance.category,
amount: balance.amount,
count: 1,
details: [transactionDetail],
})
}
}
但是排
details: [categories[categoryIndex].details].push(transactionDetail)
不能正常工作。 可能的原因是,我有时 Object 作为tyopeof
结果,有时undefined
行console.log([categories[categoryIndex].details])
有时 output:
// Output for
// console.log([categories[categoryIndex].details])
[Array(1)]0: Array(1)
0: {date: 20220414, amount: 410, source: 'xxx', destination: 'yyy'}
length: 1[[Prototype]]:
Array(0)length: 1
[[Prototype]]: Array(0)
[2]
0: 2
length: 1
[[Prototype]]: Array(0)
任何 hiths 如何将 object transactionDetail
添加为现有数组中的下一个? 非常感谢您的任何建议。
我不明白。 如果类别已经存在,我可以连接字符串,添加数字,但我不能将下一个 object 添加到对象数组中。
编辑:只是在解释中将transaction
更改为trasactionDetail
。
我在您的后一个块中发现了几个错误并已更正; 让我解释一下我所做的更改。
在您标记为错误的行中,您出于某种原因将值放在括号中。 Categories.details
大概是一个TransactionDetail
的数组,所以你不需要在这里进一步嵌套它。 但是,如果您推入一个数组,它会返回数组中对象的数量,因此当您在该行中执行此操作时,最终将始终用一个数字填充details
。 相反,在我的版本中,我将您通过索引提取的现有类别拆分为existing
类别,并将该值简单地推送到其details
数组。 这也只是清理了您条件的上半部分,因为只需要引用现有 object 中的属性即可以更简洁的方式与新值匹配。
您使用1(categories[categoryIndex].count += 1)
来增加计数。 但是,您也在此处精确设置了 object,因此这不是一个好的做法。 相反,设置您打算在此处使用的值并将其作为一件事全部提交到categories
数组,而不是此处设置的某些值不匹配,有些设置不同。 我将其更正为仅仅是existing.count + 1
。
这是您更新后的完整代码:
balances.forEach((balance) => {
// Checking if category already exists in my array of categories
let categoryIndex = categories.findIndex(category => category.category === balance.category);
// Create details of transaction
let transactionDetail = {
date: balance.date,
amount: balance.amount,
source: balance.source,
destination: balance.destination,
};
// If category already exists, just calculate total and add new object into array details
if (categoryIndex !== -1) {
const existing = categories[categoryIndex];
existing.details.push(transactionDetail);
categories[categoryIndex] = {
type: balance.type,
category: balance.category,
amount: existing.amount + balance.amount,
count: existing.count + 1,
details: existing.details
};
} else {
// If category doesn't yet exists, we must create a first values in this category
categories.push({
type: balance.type,
category: balance.category,
amount: balance.amount,
count: 1,
details: [transactionDetail],
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.