I am facing a peculiar problem while trying to filter an array in TypeScript. Here is the structure of my object:
Sigma.model.ts
export class Sigma {
sigmaId: number;
name: string;
userId: number;
starId: string;
}
``
The starId property contains comma separated IDs (e.g. 3,4).
My goal is to filter an array called allStars based on the star ids present in the Sigma object. Assuming that allStars contains objects with star ids 1,2,3,4,5, I want sigStars to only contain objects with ids 3 and 4. Below is the code snippet:
filter.component.ts
selectedFilter(filter) {
const starIds = filter.sigma.split(','); // example-> const starIds = [5,6];
for (const id in starIds) {
this.sigStars = this.allStars.filter(star => star.sid === id);
}
}
However, upon debugging, I noticed that within the for loop, the 'const id' always holds the value "0". It doesn't seem to be taking the values from starIds i.e. [3,4].
Could someone please point out what might be going wrong in my approach here.