代码之家  ›  专栏  ›  技术社区  ›  Ragavan Rajan

将数字/字符串数组传递给函数的typescript

  •  0
  • Ragavan Rajan  · 技术社区  · 6 年前

    我被这件事深深打动了。

    单一请求:

    1. 使用模型绑定,两个输入框被传递到我的typescript函数中,它接受两个参数并显示输出,这非常好。下面是我的示例函数。

      convert(X, Y) {
       this.output = X * Y;
      
      console.log("Output:", this.output);
      
      } 
      
    2. 批量请求

    另一种情况是:

    我有一个输入文本区,用户可以在其中复制和粘贴Excel/txt文件中的大量数据。我想把每一行都传递给我上面的typescript函数。我该怎么让你知道 \t and \n 我的打字脚本函数

    enter image description here

    我还为此创建了stackblitz。谁能帮忙吗。谢谢

    Stackblitz编辑器URL: https://stackblitz.com/edit/primeng-passing-srting-array-to-function

    0 回复  |  直到 6 年前
        1
  •  1
  •   paolostyle    6 年前
    function bulkConvert(text: string) {
      const pairs = text
        .split(/\n/) // split by line
        .map(pair => pair
          .split(/[\s]+/) // split by whitespace
          .map(numString => parseFloat(numString)) // parse string to number
        ); 
      const results = pairs.map(([x, y]) => x * y);
      // then do whatever you want with results, I think you want this.outputBulk = results;
    }