javascriptangulartypescript

Angular Typescript On Array.map is returning ""this.array.map is not a function"


I am using Angular 6 and previous got this following to work get all the values associated with an array property.

this.object.array.map(item => item.month).filter((value, index, self) => self.indexOf(value) === index);

In another component, the webservice is returning an array and not an object with an embedded array. The following is throwing map() is not a function which is confusing.

this.array.map(item => item.month).filter((value, index, self) => self.indexOf(value) === index);

Here is the model definition of the array

In my ts code, I declared as following

 myArrays: ArrayType[];

in my model.ts

export class ArrayType {

  constructor () {


  }
  public id: string;
  public name: string;
  public month: string;
 
}

Thanks


Solution

  • You need check the returned object from your webservice is array or not.

    I tried to reproduce your code still worked.

    https://stackblitz.com/edit/angular-array-map2