[英]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.