简体   繁体   English

在Typescript / Angular中将数组映射到对象

[英]Mapping an array to an object in Typescript/Angular

I have a service that is querying a backend API. 我有一个正在查询后端API的服务。 The API returns a set of comma delimited lines of text. API返回一组以逗号分隔的文本行。 These columns correspond to the attributes in a TypeScript class that I've created. 这些列对应于我创建的TypeScript类中的属性。 The class is: 该类是:

export class TopTalker {
    constructor(
        public source: string,
        public protocol: number,
        public packets: number,
        public _percent: string,
        public _tally: string
    ) {}
}

I am currently building objects out of these like so: 我目前正在像这样构建对象:

  getTopTalkers() {
    this._silk.topTalkers().subscribe(
        data => {
        let results = <any>[];
        results = data;
        this.topTalkers = <any>[];
        for (let row of results) {
          let thisTalker = row.split(/,/);
          let a = new TopTalker(
            thisTalker[0],
            thisTalker[1],
            thisTalker[2],
            thisTalker[3],
            thisTalker[4]);
          if(thisTalker.length > 1){   // Was getting a blank line
            this.topTalkers.push(a);
          }
        }
      console.log(this.largestTransfers);
      },
        err => console.error(err),
        () => console.log('Processed talkers')
        );
  }

Here is an example of the data returned by the API: 这是API返回的数据的示例:

["8.248.215.242,6,7037358,53.111350,53.111350","8.248.209.245,6,2459465,18.561725,71.673076","192.168.2.86,6,604136,4.559450,76.232525","192.168.2.43,6,295422,2.229567,78.462092","192.168.2.39,6,254746,1.922583,80.384675","8.248.211.241,6,182544,1.377670,81.762345","192.168.2.69,6,180162,1.359693,83.122038","8.250.125.246,6,152862,1.153658,84.275697","8.248.213.243,6,93544,0.705982,84.981679","167.206.10.209,6,69862,0.527253,85.508931",""] [“ 8.248.215.242,6,7037358,53.111350,53.111350”,“ 8.248.209.245,6,2459465,18.561725,71.673076”,“ 192.168.2.86,6,604136,4.559450,76.232525”,“ 192.168.2.43,6, 295422,2.229567,78.462092“,” 192.168.2.39,6,254746,1.922583,80.384675“,” 8.248.211.241,6,182544,1.377670,81.762345“,” 192.168.2.69,6,180162,1.359693,83.122038“,” 8.250.125.246,6,152862,1.153658,84.275697“,” 8.248.213.243,6,93544,0.705982,84.981679“,” 167.206.10.209,6,69862,0.527253,85.508931“,”“

This works, but it feels really wrong. 这行得通,但感觉确实不对。 Is there some feature of Angular or TypeScript that I am missing that would allow me to map each line to create an array of objects directly? 我是否缺少Angular或TypeScript的某些功能,使我可以映射每行以直接创建对象数组?

If I understand your code correctly, something like this should be pretty close to what you're after (and is a lot easier to read, imo) 如果我正确理解您的代码,则类似这样的内容应该与您所追求的相当接近(imo更容易阅读)

getTopTalkers() {
  this._silk.topTalkers().subscribe(
    results => {
      this.topTalkers = results.map((row, index, rows) => {
        let thisTalker = row.split(/,/);

        // Avoid blank lines by checking length
        if (thisTalker.length > 1) {
          new TopTalker(
            thisTalker[0],
            thisTalker[1],
            thisTalker[2],
            thisTalker[3],
            thisTalker[4]
          );
        }
      })
    },
    err => console.error(err),
    () => console.log('Processed talkers')
  );
}

Ideally, I'd explore getting that service updated to not return grotesque comma-separated strings. 理想情况下,我将探索更新该服务以不返回怪诞的逗号分隔字符串。

You could simplify your constructor signature a little bit by moving the mapping work into the body, like so: 您可以通过将映射工作移到主体中来稍微简化构造函数签名,如下所示:

class TopTalker {

    public source: string;
    public protocol: number;
    public packets: number;
    public _percent: string;
    public _tally: string;

    constructor(sourceRow: string[]) { 
        this.source = sourceRow[0];
        this.protocol = parseInt(sourceRow[1]);
        this.packets = parseInt(sourceRow[2]);
        this._percent = sourceRow[3];
        this._tally = sourceRow[4];
    }
}

Which would remove some of the complexity from the instantiating code: 这将消除实例化代码的一些复杂性:

for (let row of results) {
      let thisTalker = row.split(/,/);
      let a = new TopTalker(thisTalker);
      this.topTalkers.push(a);        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM