Skip to content

Instantly share code, notes, and snippets.

@jonathanmza
Forked from amcdnl/mapToIterable.js
Last active October 8, 2019 15:48
Show Gist options
  • Save jonathanmza/1c1e6e9a3c16e3f63d1f33cb95646748 to your computer and use it in GitHub Desktop.
Save jonathanmza/1c1e6e9a3c16e3f63d1f33cb95646748 to your computer and use it in GitHub Desktop.
import { Pipe, PipeTransform } from '@angular/core';
/**
* Iterable Pipe
*
* It accepts Objects and [Maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
*
* Example:
*
* <div *ngFor="let keyValuePair of someObject | iterable">
* key {{keyValuePair.key}} and value {{keyValuePair.value}}
* </div>
*
*/
@Pipe({ name: 'iterable' })
export class IterablePipe implements PipeTransform {
transform(iterable: any, args?: any[]): any {
const result = [];
if (iterable && iterable.entries) {
iterable.forEach((value, key) => {
result.push({ key, value });
});
} else {
for (const key in iterable) {
if (iterable.hasOwnProperty(key)) {
result.push({ key, value: iterable[key] });
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment