我有一个包含日期和值的对象列表。我想创建一个新的对象,该对象将对象中值的总和相加。到目前为止,我有一个解决方案,但对代码不满意,想知道您是否有更好的解决方案?
此外,我想按顺序显示最近的一年。
const dividendsYears = {
1976: [
0: {date: "1976-05-10", value: "0.00062"}
1: {date: "1976-07-16", value: "0.00062"}
2: {date: "1976-10-21", value: "0.00062"}
],
1977: [
0: {date: "1977-02-14", value: "0.00062"}
1: {date: "1977-05-12", value: "0.00123"}
2: {date: "1977-07-19", value: "0.00123"}
3: {date: "1977-10-18", value: "0.00123"}
],
1978: [
0: {date: "1978-02-14", value: "0.00123"}
1: {date: "1978-05-16", value: "0.00222"}
2: {date: "1978-07-19", value: "0.00222"}
3: {date: "1978-10-26", value: "0.00222"}
]
}
// Create new object that sum the total of value
let totalDividendsPerYear = {}
Object.entries(dividendsYears).forEach((entry, index) => {
let sum = 0
// Adds up dividends value per year
entry[1].forEach((item) => (
sum += Number(item.value)
))
totalDividendsPerYear[entry[0]] = sum
});
// Reverse array to sort the recent date to the oldest date
const sortRecentDate = Object.assign([], totalDividendsPerYear).reverse();
预期结果:
{
1978: 0.00789,
1977: 0.0043100000000000005,
1976: 0.00186
}
答案 0 :(得分:2)
您可以使用单个Object.keys和Array.reduce来做到这一点:
const data = { 1976: [ {date: "1976-05-10", value: "0.00062"}, {date: "1976-07-16", value: "0.00062"}, {date: "1976-10-21", value: "0.00062"} ], 1977: [ {date: "1977-02-14", value: "0.00062"}, {date: "1977-05-12", value: "0.00123"}, {date: "1977-07-19", value: "0.00123"}, {date: "1977-10-18", value: "0.00123"} ], 1978: [ {date: "1978-02-14", value: "0.00123"}, {date: "1978-05-16", value: "0.00222"}, {date: "1978-07-19", value: "0.00222"}, {date: "1978-10-26", value: "0.00222"} ] }
let result = Object.keys(data).reduce((r,k) => {
r[k] = data[k].reduce((acc,c) => (+acc.value || acc || 0) + (+c.value))
return r
}, {})
console.log(result)
您也可以在一行中牺牲一些可读性:
const data = { 1976: [ {date: "1976-05-10", value: "0.00062"}, {date: "1976-07-16", value: "0.00062"}, {date: "1976-10-21", value: "0.00062"} ], 1977: [ {date: "1977-02-14", value: "0.00062"}, {date: "1977-05-12", value: "0.00123"}, {date: "1977-07-19", value: "0.00123"}, {date: "1977-10-18", value: "0.00123"} ], 1978: [ {date: "1978-02-14", value: "0.00123"}, {date: "1978-05-16", value: "0.00222"}, {date: "1978-07-19", value: "0.00222"}, {date: "1978-10-26", value: "0.00222"} ] }
let result = Object.keys(data).reduce((r,k) =>
(r[k] = data[k].reduce((a,c) => (+a.value || a || 0) + (+c.value))) && r, {})
console.log(result)
答案 1 :(得分:1)
这是一个单一的解决方案:
const dividendsYears = {
"1976": [
{date: "1976-05-10", value: "0.00062"},
{date: "1976-07-16", value: "0.00062"},
{date: "1976-10-21", value: "0.00062"}
],
"1977": [
{date: "1977-02-14", value: "0.00062"},
{date: "1977-05-12", value: "0.00123"},
{date: "1977-07-19", value: "0.00123"},
{date: "1977-10-18", value: "0.00123"}
],
"1978": [
{date: "1978-02-14", value: "0.00123"},
{date: "1978-05-16", value: "0.00222"},
{date: "1978-07-19", value: "0.00222"},
{date: "1978-10-26", value: "0.00222"}
]
}
const result = Object.entries(dividendsYears).reduce((a,b) => { a[b[0]] = b[1].map(i => parseFloat(i.value)).reduce((k,j) => k + j); return a; },{});
document.write(JSON.stringify(result));
答案 2 :(得分:1)
我想我得到一个较短的:;)
const dividendsYears = {
1976: [ { date: "1976-05-10", value: "0.00062" }
, { date: "1976-07-16", value: "0.00062" }
, { date: "1976-10-21", value: "0.00062" }
],
1977: [ { date: "1977-02-14", value: "0.00062" }
, { date: "1977-05-12", value: "0.00123" }
, { date: "1977-07-19", value: "0.00123" }
, { date: "1977-10-18", value: "0.00123" }
],
1978: [ { date: "1978-02-14", value: "0.00123" }
, { date: "1978-05-16", value: "0.00222" }
, { date: "1978-07-19", value: "0.00222" }
, { date: "1978-10-26", value: "0.00222" }
]
};
const result = Object.entries(dividendsYears).reduce((r,x)=>{r[x[0]]=x[1].reduce((a,c)=>a+parseFloat(c.value),0); return r},{});
console.log( result )