I created a class, that extends Array:
export class Collection<T> extends Array<T> {
constructor(items?: Array<T>) {
super(...items);
Object.setPrototypeOf(this, Object.create(Collection.prototype));
}
whereKey(key: string, value: any): T {
for(let i = 0; i < this.length; i++) {
if(this[i][key] === value) {
return this[i];
}
}
}
removeAt(index: number) {
this.splice(index, 1);
}
}
However, using anything standard array method, like splice(), or even filter(), doesn't work. I get ImportsHomeComponent.html:15 ERROR TypeError: CreateListFromArrayLike called on non-object as an error. Literally the only method I can use is push.
I have created the collection like so:
getImportTests(): Observable<Collection<ImportTest>> {
return this.http.get<Collection<IImportTest>>(route('imports/import-tests')).map((importTests: Collection<IImportTest>) => {
return new Collection<ImportTest>(importTests.map((importTest: IImportTest) => {
return new ImportTest(importTest);
}));
});
}
And then tried to remove an element like so:
this.importTests.removeAt(this.importTests.indexOf(importTest));
I only really need to use a collection but of the whereKey method, but I'm open to anything as this is driving me crazy.
findwork as well?