【问题标题】:using the map operator in angular 4 for object assignment使用角度 4 中的 map 运算符进行对象分配
【发布时间】:2018-03-24 09:42:27
【问题描述】:

我需要使用数组将一个对象映射到另一个对象。我如何在角度 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);
    }
  }

我正在努力实现以下目标。请注意,结果应包含charteries 和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>

【问题讨论】:

  • 什么是chartSeries和yAxisSeries? map 必须退回这个吗?
  • 是的,样条图组件需要这两个对象。我更新了帖子以显示绑定是如何完成的。见代码行
  • 我认为我的解决方案将返回结果对象,我可以这样做
  • 你可以用这个var result = this.result.reduce((r,o) =&gt; { if(o.data) r.chartSeries.push({data: o.data, name: o.seriesName, color: o.color}); if(o.yaxis) r.yAxisSeries.push({yaxis: o.yaxis}); return r; },{chartSeries: [], yAxisSeries: []})
  • 我收到以下错误 compiler.es5.js:14985 ERROR TypeError: Cannot read property 'map' of undefined at SplineChartComponent.webpackJsonp.../../../../。 ./src/app/shared/Highcharts/spline/spline-

标签: angular highcharts


【解决方案1】:

试试这个。

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

【讨论】:

  • 请注意我的结果对象应该同时包含对象charteries和yaxisarray
猜你喜欢
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
相关资源
最近更新 更多