英文:
How to filter/sum array data by year/quarter?
问题
尝试从此链接的对象中按年份和季度汇总股息数据。我已经能够从对象中提取股息日期和股息金额,并使用以下代码将股息日期转换为YYYY Q格式。从结果中,我想删除2018年和2019年之外的任何股息数组,并仅在2018年和2019年之间按年份和季度汇总剩余部分,就像这张图片一样。如果在特定季度没有股息数据,我想将其设为0,并将特定季度中的多个股息金额相加。我该如何实现这一点?
function test() {
const url = 'https://query1.finance.yahoo.com/v8/finance/chart/BAYRY?formatted=true&lang=en-US®ion=US&interval=1d&period1=1451624400&period2=1577854799&events=div&useYfid=true&corsDomain=finance.yahoo.com';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();;
const obj = JSON.parse(res);
const dividend_id = Object.keys(obj.chart.result[0].events.dividends);
var dividend = dividend_id.map((id => [
obj.chart.result[0].events.dividends[id].date,
obj.chart.result[0].events.dividends[id].amount]));
var dividend = dividend.map(arr => [new Date(arr[0] * 1000).toLocaleDateString('en-US'), arr[1]]);
console.log(dividend)
var dividend = dividend.map(arr => [(arr[0].slice(-4) + ' Q' + (getQuarter(new Date(arr[0])))), arr[1]]);
console.log(dividend)
function getQuarter(date = new Date()) {
return Math.floor(date.getMonth() / 3 + 1);
}
}
这是你提供的代码的翻译。如果你需要进一步的帮助,请随时告诉我。
<details>
<summary>英文:</summary>
Trying to consolidate dividend data by year and quarter from an object from this [URL][1].
I was able to retrieve dividend date and dividend amount from the object and convert the dividend date to YYYY Q format with the following codes. From the results, I like to remove any dividend arrays outside of year 2018 and 2019, and consolidate the remainder by year and quarter only between 2018 and 2019, like this picture. I like to put 0 if no dividend data in a given quarter, and sum any multiple dividend amounts in a given quarter. How can I achieve that?
[![enter image description here][2]][2]
function test() {
const url = 'https://query1.finance.yahoo.com/v8/finance/chart/BAYRY?formatted=true&lang=en-US®ion=US&interval=1d&period1=1451624400&period2=1577854799&events=div&useYfid=true&corsDomain=finance.yahoo.com';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();;
const obj = JSON.parse(res);
const dividend_id = Object.keys(obj.chart.result[0].events.dividends);
var dividend = dividend_id.map((id => [
obj.chart.result[0].events.dividends[id].date,
obj.chart.result[0].events.dividends[id].amount]));
var dividend = dividend.map(arr => [new Date(arr[0] * 1000).toLocaleDateString('en-US'), arr1]);
console.log(dividend)
var dividend = dividend.map(arr => [(arr[0].slice(-4) + ' Q' + (getQuarter(new Date(arr[0])))), arr1]);
console.log(dividend)
function getQuarter(date = new Date()) {
return Math.floor(date.getMonth() / 3 + 1);
}
}
[1]: https://query1.finance.yahoo.com/v8/finance/chart/BAYRY?formatted=true&lang=en-US&region=US&interval=1d&period1=1451624400&period2=1577854799&events=div&useYfid=true&corsDomain=finance.yahoo.com
[2]: https://i.stack.imgur.com/tuqa6.png
</details>
# 答案1
**得分**: 1
这是我会做的方式。我将使用日期获取年份,并为每一年构建一个数组 `[年份,0,0,0,0]`。然后使用年份找到该年份的数组,使用月份作为索引来找到季度。
```javascript
function getDividends() {
const url = 'https://query1.finance.yahoo.com/v8/finance/chart/BAYRY?formatted=true&lang=en-US&region=US&interval=1d&period1=1451624400&period2=1577854799&events=div&useYfid=true&corsDomain=finance.yahoo.com';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();;
const obj = JSON.parse(res);
const dividend_id = Object.keys(obj.chart.result[0].events.dividends);
var dividend = dividend_id.map(id => [
obj.chart.result[0].events.dividends[id].date,
obj.chart.result[0].events.dividends[id].amount
]);
let quarters = [];
dividend.forEach(item => {
let day = new Date(item[0] * 1000);
let year = day.getFullYear();
if (year > 2017) {
let i = quarters.findIndex(quarter => quarter[0] === year);
if (i < 0) {
quarters.push([year, 0, 0, 0, 0]);
i = quarters.length - 1;
}
let j = Math.floor(day.getMonth() / 4) + 1;
quarters[i][j] = quarters[i][j] + item[1];
}
});
dividend = [];
quarters.forEach(quarter => {
for (let i = 1; i < 5; i++) {
dividend.push([(quarter[0] + " Q" + i), quarter[i]]);
}
});
console.log(dividend);
}
执行日志:
9:33:08 AM Notice Execution started
9:33:09 AM Info
[ [ '2018 Q1', 0 ],
[ '2018 Q2', 1.2289999999999999 ],
[ '2018 Q3', 0 ],
[ '2018 Q4', 0 ],
[ '2019 Q1', 0.789 ],
[ '2019 Q2', 0 ],
[ '2019 Q3', 0 ],
[ '2019 Q4', 0 ] ]
9:33:09 AM Notice Execution completed
参考链接:
英文:
Here is how I would do it. I would use the Date to get the year and build an array for each year [year,0,0,0,0]
. Then use the year to find the array for that year and the month to find the quarter as index into the arrays.
function getDividends() {
const url = 'https://query1.finance.yahoo.com/v8/finance/chart/BAYRY?formatted=true&lang=en-US&region=US&interval=1d&period1=1451624400&period2=1577854799&events=div&useYfid=true&corsDomain=finance.yahoo.com';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();;
const obj = JSON.parse(res);
const dividend_id = Object.keys(obj.chart.result[0].events.dividends);
var dividend = dividend_id.map((id => [
obj.chart.result[0].events.dividends[id].date,
obj.chart.result[0].events.dividends[id].amount]));
let quarters = [];
dividend.forEach( item => {
let day = new Date(item[0]*1000);
let year = day.getFullYear();
if( year > 2017 ) {
let i = quarters.findIndex( quarter => quarter[0] === year );
if( i < 0 ) {
quarters.push([year,0,0,0,0]);
i = quarters.length-1;
}
let j = Math.floor(day.getMonth()/4)+1;
quarters[i][j] = quarters[i][j]+item[1];
}
}
);
dividend = [];
quarters = quarters.forEach( quarter => {
for( let i=1; i<5; i++ ) {
dividend.push([(quarter[0]+" Q"+i),quarter[i]]);
}
}
)
console.log(dividend);
}
Execution log
9:33:08 AM Notice Execution started
9:33:09 AM Info
[ [ '2018 Q1', 0 ],
[ '2018 Q2', 1.2289999999999999 ],
[ '2018 Q3', 0 ],
[ '2018 Q4', 0 ],
[ '2019 Q1', 0.789 ],
[ '2019 Q2', 0 ],
[ '2019 Q3', 0 ],
[ '2019 Q4', 0 ] ]
9:33:09 AM Notice Execution completed
Reference
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论