使用角度4中的map运算符进行对象分配

时间:2018-03-24 09:42:27

标签: angular highcharts

我需要使用数组将一个对象映射到另一个对象。我如何在角度4中实现它。目前我使用forEach循环将项目推送到数组。我需要使用地图。

     export interface GraphConfiguration  {

            seriesName: string;
            color: string;
        }
        export interface StressTestAnalysis extends GraphConfiguration {

            curlevel: number;
            decrease: number;
            increase: number;
            yaxis: number[];
            data: number[];
        }

        public results: Array<StressTestAnalysis> = [];
        private _stressResults: Array<StressTestAnalysis> = [];

         @Input() set stressResults(value: Array<StressTestAnalysis>) {
            this._stressResults = value;
            this.addSeries();
            let minY = Math.min(...this.yAxisSeries.map(el => Math.min(...el.yaxis)));
            let maxY = Math.max(...this.yAxisSeries.map(el => Math.max(...el.yaxis)));
    this.generateYAxisArray(minY, maxY);
         }

            this.results.forEach(element => {
                  if (element.data !== null)
                    this.chartSeries.push({ data: element.data, name: element.seriesName, color: element.color });
                   if (element.yaxis !== null)
                    this.yAxisSeries.push({ yaxis: element.yaxis });
                });

private addSeries() {
    if (this._stressResults === null) {
      return;
    }

 private generateYAxisArray(min: any, max: any) {
    let count = min;
    for (count = min; count <= max; count = count + 500000) {
      this.yAxisData.push(count);
    }
  }

我正在努力实现以下目标。请注意,结果应包含chartseries和yaxisseries。我正在考虑删除foreach循环,因为我认为它导致重复。

 this.results =  this._stressResults.map((result: any) => {

   return;
    });

直方图组件代码

  <div *ngIf="!showTable" class="tab-pane base-strategy-chart fade show active" id="base-strategy-chart--nva" role="tabpanel"
          aria-labelledby="chart-tab">
          <div class="tb-container">

            <div class="tb-row d-flex flex-row">
              <div class="tb-cell col-12 pt-5">
               <splinechart [series]="chartSeries" [yaxisdata]="yAxisData">
               </splinechart>
                </div>

            </div>
          </div>

1 个答案:

答案 0 :(得分:0)

试试这个。

    var numbers = [1, 4, 9];
    this.results =  this.numbers.map((result) => {
           console.log('Your result:', result);
           // Add your business logic to modify result
           new_result = result + 1
           return new_result;
    });
    console.log('Your final results array:', this.results);