Trying to implement a pipe in Angular. After realizing ngFor wouldn't work with maps. Some research led me to believe that future features were coming to deal with that but in the meantime a mapToIterable pipe was the answer.
I have the following code:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'mapToIterable'
})
export class MapToIterablePipe implements PipeTransform {
transform(map: Map<string, Object>, args: any = []): any {
const a: any[] = [];
console.log(map.keys()); // <- works as expected
for (const k in map) {
if (map.has(k)) {
console.log("hello"); // <- never executes
console.log(k);
a.push({key: k, val: map.get(k)});
}
}
console.log(a); // <- always empty
return a;
}
}
export const MAPTOITERABLE_PROVIDERS = [
MapToIterablePipe
];
map.keys() gives me a list of correct keys, but nothing else works.
Any suggestion on how to diagnose why my loop isn't filling the array correctly?
Map
'keys' are not object keys and cannot be obtained withObject.keys()
orin
operator.Considering that
map.keys()
returns an iterable, it should beOr in TypeScript 2.3 with
downlevelIteration
option,Or just
Due to how TypeScript implements spread operator,
[...iterable]
won't work the same way asArray.from(iterable)
in 2.2 and lower.