gpt4 book ai didi

javascript - Javascript从具有相同属性的不同对象创建公共(public)对象,并具有相同的键并添加金额

转载 作者:行者123 更新时间:2023-12-02 22:41:31 27 4
gpt4 key购买 nike

I am trying to get data from JSON :

   {
"data": {
"Country": [
{
"Area": "Urban",
"Date": "2019-10-14T12:14:20.170Z",
"income": [
{
"amount": "33",
"currency": "USD"
},
{
"amount": "10",
"currency": "INR"
}
],
"expenditure": [
{
"amount": "5",
"currency": "INR"
}
],
"tax": [
{
"amount": "10",
"currency": "USD"
},
{
"amount": "10",
"currency": "INR"
}
]
},
{
"Area": "Rural",
"Date": "2019-10-14T12:14:20.170Z",
"income": [
{
"amount": "2",
"currency": "USD"
},
{
"amount": "20",
"currency": "INR"
}
],
"loan": [
{
"amount": "5",
"currency": "INR"
}
],
"tax": [
{
"amount": "10",
"currency": "USD"
},
{
"amount": "50",
"currency": "INR"
}
]
}
]
}
}

I want to have a common object which hold data with all unique key such as income,expenditure and then sum the amount which has same currency.

需要以下格式的结果:

{
"Area": "combined",
"income": [
{
"amount": "35",
"currency": "USD"
},
{
"amount": "30",
"currency": "INR"
}
],
"expenditure": [
{
"amount": "5",
"currency": "INR"
}
],
"loan": [
{
"amount": "5",
"currency": "INR"
}
],
"tax": [
{
"amount": "20",
"currency": "USD"
},
{
"amount": "60",
"currency": "INR"
}
]
}

如果尝试使用map和reduce,但如果不使用if else语句来检查对象内部是否存在值,然后执行操作,我就无法进行某些操作。

我尝试了以下方法:

jsonResponse.reduce(
// common_keys passing as hardcoded till loan ,tax etc
(result, { loan,tax}) => {
forEach(tax, value => {
const taxObj = find(result.tax, ['currency', value.currency]);
!taxObj ? result.tax.push(value) : taxObj.amount = Number(taxObj.amount) + Number(value.amount);
});

forEach(loan, value => {
const loanObj = find(result.loan, [
'currencyCode',
value.currency,
]);
!loanObj ? result.loan.push(value): loanObj.amount = Number(loanObj.amount) + Number(value.amount);
});
//repeating for other too

return result;
}

最佳答案

使用所有相同的currencyCode,您也可以减少数组和嵌套数组。

var data = { data: { Country: [{ Area: "Urban", Date: "2019-10-14T12:14:20.170Z", income: [{ amount: "33", currencyCode: "USD" }, { amount: "10", currencyCode: "INR" }], expenditure: [{ amount: "5", currencyCode: "INR" }], tax: [{ amount: "10", currencyCode: "USD" }, { amount: "10", currencyCode: "INR" }] }, { Area: "Rural", Date: "2019-10-14T12:14:20.170Z", income: [{ amount: "2", currencyCode: "USD" }, { amount: "20", currencyCode: "INR" }], loan: [{ amount: "5", currencyCode: "INR" }], tax: [{ amount: "10", currencyCode: "USD" }, { amount: "50", currencyCode: "INR" }] }] } },
result = data.data.Country.reduce((r, o) => {
Object.entries(o).forEach(([k, v]) => {
if (!Array.isArray(v)) return;
v.reduce((q, { amount, currencyCode }) => {
var temp = q.find(t => t.currencyCode === currencyCode);
if (!temp) q.push(temp = { amount: 0, currencyCode });
temp.amount = (+temp.amount + +amount).toString();
return q;
}, r[k] = r[k] || []);
});
return r;
}, { "Area": "combined" });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - Javascript从具有相同属性的不同对象创建公共(public)对象,并具有相同的键并添加金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58569371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com