通过可观察的AlphaVantage进行迭代

问题描述 投票:0回答:2

我想遍历一个Observable,但是返回值需要一个键。我如何遍历时间序列并获取开盘价,高价,低价,收盘价和交易量的值。这是打字稿界面。

export interface DailyPrices {
  'Meta Data': MetaData;
  'Time Series (Daily)': { [key: string]: TimeSeriesDaily };
}

export interface MetaData {
  '1. Information': string;
  '2. Symbol': string;
  '3. Last Refreshed': Date;
  '4. Output Size': string;
  '5. Time Zone': string;
}

export interface TimeSeriesDaily {
  '1. open': string;
  '2. high': string;
  '3. low': string;
  '4. close': string;
  '5. volume': string;
}

这里是服务方法。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DailyPrices } from '../ApplicationModels/DailyTimeseries';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class PricingService {
  constructor(private http: HttpClient) {}

  rootUrl =
    `https://www.alphavantage.co/query?` +
    `function=TIME_SERIES_DAILY&symbol=MSFT&apikey=5FIPVHFVW9SX0IF4`;

  getDailyPricing(): Observable<DailyPrices> {
    return this.http.get<DailyPrices>(this.rootUrl);
  }
}

这里是我希望在其中收集每个时间序列数据点的特定值的组件。

export class HomepageComponent implements OnInit {
  constructor(private pricingService: PricingService) {}
  dates: DailyPrices;
  open: string;
  homepageChart: any;
  lineChartOptions = {
    responsive: true
  };
  lineChartType = 'line';

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.pricingService.getDailyPricing().subscribe(data => {
      this.dates = data;
    });
  }
}
angular typescript observable angular-services angular-httpclient
2个回答
0
投票

要从对象中获取值,可以在键上循环:

const data = {
  'Meta Data': {
    '1. Information': 'info',
    '2. Symbol': 'symbol',
    '3. Last Refreshed': '2020-05-40',
    '4. Output Size': 'output',
    '5. Time Zone': 'time zone'
  },
  'Time Series (Daily)': {
    someKey: {
      '1. open': 'someKey#open',
      '2. high': 'someKey#high',
      '3. low': 'someKey#open',
      '4. close': 'someKey#close',
      '5. volume': 'someKey#volume',
    },
    someOtherKey: {
      '1. open': 'someOtherKey#open',
      '2. high': 'someOtherKey#high',
      '3. low': 'someOtherKey#open',
      '4. close': 'someOtherKey#close',
      '5. volume': 'someOtherKey#volume',
    }
  }
};
const timeSeries = data['Time Series (Daily)'];
const parentKeys = Object.keys(timeSeries);

parentKeys.forEach(parentKey => {
  const parentValue = timeSeries[parentKey];
  const childKeys = Object.keys(parentValue);
  
  console.group(`Values for ${parentKey}:`);

  childKeys.forEach(childKey => {
    console.log({ key: childKey, value: parentValue[childKey] });
  });

  console.groupEnd();
});

请注意,对于此示例,我遵循提供的接口的形状。


0
投票

您实际上可以保留界面以及您所拥有的一切。

这将是一种迭代获得的数据的方法:

this.pricing.getDailyPricing().subscribe(data => {
  Object.entries(data['Time Series (Daily)']).forEach(([date, timeSeriesDaily])=> {
    console.log(date); // 2019-11-05
    console.log(timeSeriesDaily); // {1. open: "143.8400", 2. high: "144.8800", 3. low: "143.7700", 4. close: "144.2600"…}
    console.log(timeSeriesDaily['1. open']); // 143.8400
  });
});

此外,我也不建议您在此之后刷新您的API密钥,因为共享它不是一个好主意。

© www.soinside.com 2019 - 2024. All rights reserved.