Trying to consolidate dividend data by year and quarter from an object from this URL. 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?
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);
}
}