-
-
Save jonathanmza/1c1e6e9a3c16e3f63d1f33cb95646748 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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