错误TypeError:array.map不是角管中的函数

时间:2019-12-22 11:06:59

标签: javascript angular angular8 angular-pipe

我收到此错误ERROR TypeError:错误TypeError:array.map在使用此角形管道时不是函数错误,这是代码。

kernel void array_sum(device float *array,
                      device int &len,
                      uint index [[ thread_position_in_grid ]])
{
    int i = (index << 1);
    int j = i + 1;
    int k;
    while (isnan(array[i]) || isnan(array[j])) {
        k = i; i = j; j = k; // timeout
    }
    array[index + len] = array[i] + array[j];
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您可能没有在数组上调用此管道。

理想情况下,如果到管道的input的格式不正确,则应确保抛出错误。这样,它将以更优雅的方式实现

对管道进行以下更改以实现此目的:

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
  name: "joiner"
})
export class JoinerPipe implements PipeTransform {
  transform(arrayToJoin: Array<any>, joinBy?: string) {
    if (arrayToJoin && arrayToJoin.map) {
      return arrayToJoin.map(item => item.name).join(joinBy || ", ");
    }
    throw new Error('joiner pipe only expects an Array as a first argument');
  }
}

然后您可以像这样使用它:

<p> With Joiner Value - {{ items | joiner: ' and ' }}</p>
<p> Without Joiner Value - {{ items | joiner }}</p>

<p> With wrong Input that throws error on the console - {{ someOtherItems | joiner: ', ' }}</p>

关于看起来像这样的数据:

items = [
  { name: 'A' },
  { name: 'B' },
  { name: 'C' },
  { name: 'D' }
];

someOtherItems = 'ABCD';

  

这是您推荐的Working Sample Demo Code