如何找到一系列历史价格数据对象的每年的最小值和最大值

发布于 2025-01-25 09:14:23 字数 1457 浏览 3 评论 0原文

2800: {Date: '2022-01-14', Open: 514.48999, High: 522.570007, Low: 511.269989, Close: 520.599976, …}
2801: {Date: '2022-01-18', Open: 510.390015, High: 519.390015, Low: 504.980011, Close: 513.340027, …}
2802: {Date: '2022-01-19', Open: 516.47998, High: 528.919983, Low: 515.299988, Close: 516.580017, …}
2803: {Date: '2022-01-20', Open: 522.380005, High: 532.030029, Low: 509.640015, Close: 510.850006, …}
2804: {Date: '2022-01-21', Open: 508.5, High: 513.859985, Low: 499.269989, Close: 499.910004, …}
2805: {Date: '2022-01-24', Open: 491.070007, High: 520.429993, Low: 483.309998, Close: 519.659973, …}
2806: {Date: '2022-01-25', Open: 505.51001, High: 514.26001, Low: 500.01001, Close: 502.720001, …}

我拥有以下ADBE的历史价格数据超过10年。我试图根据每年11月的报告日期,每年获得最小值和最高价值。

我正在尝试将输出在数组中获取: [2010 Min Price,2011 Min Price,2012 Min Price ...]

const startDate = new Date("2010-12-01");
const endDate = new Date("2022-04-30");
    
const data2 = data.filter(a => {
    const date = new Date(a.Date);
      return (date >= startDate && date <= endDate);
    });
console.log(data)

    
function findMinMax(key) {
    const datas = data2.map((node) => node[key]);
        return {
          min: Math.min(...datas),
          max: Math.max(...datas),
        }
}
const checkit = findMinMax('Close')

根据代码,我该怎么做,而不必更改每年的开始日期和结束日期才能获得最小值最大值?

2800: {Date: '2022-01-14', Open: 514.48999, High: 522.570007, Low: 511.269989, Close: 520.599976, …}
2801: {Date: '2022-01-18', Open: 510.390015, High: 519.390015, Low: 504.980011, Close: 513.340027, …}
2802: {Date: '2022-01-19', Open: 516.47998, High: 528.919983, Low: 515.299988, Close: 516.580017, …}
2803: {Date: '2022-01-20', Open: 522.380005, High: 532.030029, Low: 509.640015, Close: 510.850006, …}
2804: {Date: '2022-01-21', Open: 508.5, High: 513.859985, Low: 499.269989, Close: 499.910004, …}
2805: {Date: '2022-01-24', Open: 491.070007, High: 520.429993, Low: 483.309998, Close: 519.659973, …}
2806: {Date: '2022-01-25', Open: 505.51001, High: 514.26001, Low: 500.01001, Close: 502.720001, …}

I have the following historical price data of ADBE spanning more than 10 years. I am trying to get the min and max value every year according to their reporting date, period ending Nov each year.

I am trying to get the output in an array:
[2010 min price, 2011 min price, 2012 min price ...]

const startDate = new Date("2010-12-01");
const endDate = new Date("2022-04-30");
    
const data2 = data.filter(a => {
    const date = new Date(a.Date);
      return (date >= startDate && date <= endDate);
    });
console.log(data)

    
function findMinMax(key) {
    const datas = data2.map((node) => node[key]);
        return {
          min: Math.min(...datas),
          max: Math.max(...datas),
        }
}
const checkit = findMinMax('Close')

As per the code, how can I do it without having to change the start and end date for each year just to get the min max value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2025-02-01 09:14:23

检查下面的解决方案根据报告月计算所有字段的最小/最大。

const data = [
  { Date: "2022-01-14", Open: 100, High: 500, Low: 50, Close: 1000 },
  { Date: "2021-12-05", Open: 200, High: 150, Low: 150, Close: 2000 },
  { Date: "2021-10-21", Open: 300, High: 550, Low: 70, Close: 200 },
  { Date: "2021-02-18", Open: 250, High: 600, Low: 10, Close: 500 },
];

const START_REPORTING_MONTH = 11; // 11 for December

const output = data.reduce((res, { Date: date, ...rest }) => {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = d.getMonth();
  let reportingPeriod;

  if (month >= START_REPORTING_MONTH) {
    reportingPeriod = `${year}-${year + 1}`;
  } else {
    reportingPeriod = `${year - 1}-${year}`;
  }

  if (!res[reportingPeriod]) {
    res[reportingPeriod] = { min: { ...rest }, max: { ...rest } };
  }

  Object.entries(rest).forEach(([k, v]) => {
    if (v < res[reportingPeriod].min[k]) {
      res[reportingPeriod].min[k] = v;
    }
    if (v > res[reportingPeriod].max[k]) {
      res[reportingPeriod].max[k] = v;
    }
  });

  return res;
}, {});

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

Checkout the solution below it calculates min/max for all fields based on the reporting month.

const data = [
  { Date: "2022-01-14", Open: 100, High: 500, Low: 50, Close: 1000 },
  { Date: "2021-12-05", Open: 200, High: 150, Low: 150, Close: 2000 },
  { Date: "2021-10-21", Open: 300, High: 550, Low: 70, Close: 200 },
  { Date: "2021-02-18", Open: 250, High: 600, Low: 10, Close: 500 },
];

const START_REPORTING_MONTH = 11; // 11 for December

const output = data.reduce((res, { Date: date, ...rest }) => {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = d.getMonth();
  let reportingPeriod;

  if (month >= START_REPORTING_MONTH) {
    reportingPeriod = `${year}-${year + 1}`;
  } else {
    reportingPeriod = `${year - 1}-${year}`;
  }

  if (!res[reportingPeriod]) {
    res[reportingPeriod] = { min: { ...rest }, max: { ...rest } };
  }

  Object.entries(rest).forEach(([k, v]) => {
    if (v < res[reportingPeriod].min[k]) {
      res[reportingPeriod].min[k] = v;
    }
    if (v > res[reportingPeriod].max[k]) {
      res[reportingPeriod].max[k] = v;
    }
  });

  return res;
}, {});

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文