typescript 将 numbers/string 的数组传递给函数

typescript passing array of numbers/string to function

我对此有点印象。

单个请求:

  1. 使用模型绑定两个输入框已传递到我的打字稿函数,它接受两个参数并显示输出,效果非常好。下面是我的示例函数。

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

另一种情况是:

我有一个输入文本区域,用户可以在其中复制和粘贴 Excel / txt 文件中的大量数据。我想将每一行传递给我上面的打字稿函数。如何让 \t and \n 成为我的打字稿函数

我还为此创建了 stackblitz。任何人都可以帮忙。谢谢。

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

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