如何在Javascript中基于对象键从对象数组添加对象值
我想根据另一个对象键从对象数组中添加一些对象值。我想知道如何通过普通Javascript或使用像lodash这样的助手库来实现这一点 我已经尝试过使用lodash的如何在Javascript中基于对象键从对象数组添加对象值,javascript,Javascript,我想根据另一个对象键从对象数组中添加一些对象值。我想知道如何通过普通Javascript或使用像lodash这样的助手库来实现这一点 我已经尝试过使用lodash的.groupBy和Array.prototype.reduce(),但还没有成功。以下是数据的外观: { "2019-01-04": [ { "payments": [ { "sum": "12", "currency"
.groupBy
和Array.prototype.reduce()
,但还没有成功。以下是数据的外观:
{
"2019-01-04": [
{
"payments": [
{
"sum": "12",
"currency": "€"
}
]
}
],
"2019-01-06": [
{
"payments": [
{
"sum": "50",
"currency": "€"
},
{
"sum": "30",
"currency": "£"
},
{
"sum": "40",
"currency": "Lek"
},
{
"sum": "2",
"currency": "£"
},
{
"sum": "60",
"currency": "£"
}
]
}
]
}
我希望sum属性从该日期起具有相同类型的所有货币的总和:
{
"2019-01-04": [
{
"payments": [
{
"sum": "12",
"currency": "€"
}
]
}
],
"2019-01-06": [
{
"payments": [
{
"sum": "50",
"currency": "€"
},
{
"sum": "92",
"currency": "£"
},
{
"sum": "40",
"currency": "Lek"
}
]
}
]
}
给定您提供的数据结构,使用以下方法可获得所需的输出:
function sumByCurrency(history) {
_.forOwn(history, value => {
const newPayments = [];
_.forEach(value["0"].payments, v => {
let existingPayment = _.find(
newPayments,
newPayment => newPayment && newPayment.currency === v.currency
);
if (existingPayment) {
let existingSum = +existingPayment.sum;
let incomingSum = +v.sum;
existingSum += incomingSum ? incomingSum : 0;
existingPayment.sum = "" + existingSum;
} else {
newPayments.push({
currency: v.currency,
sum: v.sum ? v.sum : 0
});
}
});
value["0"].payments = newPayments;
});
return history;
}
使用它传递您的对象,假设您将其称为paymentHistory
tosumByCurrency
函数,如下所示:
sumByCurrency(paymentHistory);
请注意:如果
值[“0”],您可能需要进行一些回退/确保它不会中断。付款不可用。给定您提供的数据结构,使用以下方法可提供所需的输出:
function sumByCurrency(history) {
_.forOwn(history, value => {
const newPayments = [];
_.forEach(value["0"].payments, v => {
let existingPayment = _.find(
newPayments,
newPayment => newPayment && newPayment.currency === v.currency
);
if (existingPayment) {
let existingSum = +existingPayment.sum;
let incomingSum = +v.sum;
existingSum += incomingSum ? incomingSum : 0;
existingPayment.sum = "" + existingSum;
} else {
newPayments.push({
currency: v.currency,
sum: v.sum ? v.sum : 0
});
}
});
value["0"].payments = newPayments;
});
return history;
}
const result = {};
Object.keys(input).forEach(date => {
result[date] = [{ }];
result[date][0].payments = input[date][0].payments.reduce((payments, c) => {
const grp = payments.find(p => p.currency === c.currency);
grp ? grp.sum = +grp.sum + +c.sum : payments.push(c);
return payments;
}, []);
});
使用它传递您的对象,假设您将其称为paymentHistory
tosumByCurrency
函数,如下所示:
sumByCurrency(paymentHistory);
请注意:如果value[“0”],您可能需要进行一些回退/确保它不会中断.payments
不可用。请将您的尝试添加到问题中…重述:如果某一货币在某一日期存在多次,则获取金额?您知道正手操作的货币是什么吗?请将您的尝试添加到问题中…重述:如果某一货币在某一日期存在多次,则获取金额?您知道正手操作的货币是什么吗你有什么货币?我喜欢你的解决方案,因为它比我的更干净:)谢谢!我喜欢你的解决方案,因为它比我的更干净:)谢谢!
const result = {};
Object.keys(input).forEach(date => {
result[date] = [{ }];
result[date][0].payments = input[date][0].payments.reduce((payments, c) => {
const grp = payments.find(p => p.currency === c.currency);
grp ? grp.sum = +grp.sum + +c.sum : payments.push(c);
return payments;
}, []);
});