如何按年/季度筛选/汇总数组数据?

huangapple go评论97阅读模式
英文:

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&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]));

  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&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]));

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&amp;lang=en-US&amp;region=US&amp;interval=1d&amp;period1=1451624400&amp;period2=1577854799&amp;events=div&amp;useYfid=true&amp;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&amp;lang=en-US&amp;region=US&amp;interval=1d&amp;period1=1451624400&amp;period2=1577854799&amp;events=div&amp;useYfid=true&amp;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 = &#39;https://query1.finance.yahoo.com/v8/finance/chart/BAYRY?formatted=true&amp;lang=en-US&amp;region=US&amp;interval=1d&amp;period1=1451624400&amp;period2=1577854799&amp;events=div&amp;useYfid=true&amp;corsDomain=finance.yahoo.com&#39;;
  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 =&gt; [
    obj.chart.result[0].events.dividends[id].date,
    obj.chart.result[0].events.dividends[id].amount]));

  let quarters = [];

  dividend.forEach( item =&gt; {
      let day = new Date(item[0]*1000);
      let year = day.getFullYear();
      if( year &gt; 2017 ) {
        let i = quarters.findIndex( quarter =&gt; quarter[0] === year );
        if( i &lt; 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 =&gt; {
      for( let i=1; i&lt;5; i++ ) {
        dividend.push([(quarter[0]+&quot; Q&quot;+i),quarter[i]]);
      }
    }
  )
  console.log(dividend);
}

Execution log

9:33:08 AM	Notice	Execution started
9:33:09 AM	Info	
  [ [ &#39;2018 Q1&#39;, 0 ],
  [ &#39;2018 Q2&#39;, 1.2289999999999999 ],
  [ &#39;2018 Q3&#39;, 0 ],
  [ &#39;2018 Q4&#39;, 0 ],
  [ &#39;2019 Q1&#39;, 0.789 ],
  [ &#39;2019 Q2&#39;, 0 ],
  [ &#39;2019 Q3&#39;, 0 ],
  [ &#39;2019 Q4&#39;, 0 ] ]
9:33:09 AM	Notice	Execution completed

Reference

huangapple
  • 本文由 发表于 2023年1月8日 23:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定