Created
April 13, 2016 23:26
-
-
Save amcdnl/202596c5b85cc66d7002d10bde3ab514 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 } from 'angular2/core.js'; | |
/** | |
* Map to Iteratble Pipe | |
* | |
* It accepts Objects and [Maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) | |
* | |
* Example: | |
* | |
* <div *ngFor="#keyValuePair of someObject | mapToIterable"> | |
* key {{keyValuePair.key}} and value {{keyValuePair.value}} | |
* </div> | |
* | |
*/ | |
@Pipe({ name: 'mapToIterable' }) | |
export class MapToIterable { | |
transform(value) { | |
let result = []; | |
if(value.entries) { | |
for (var [key, value] of value.entries()) { | |
result.push({ key, value }); | |
} | |
} else { | |
for(let key in value) { | |
result.push({ key, value: value[key] }); | |
} | |
} | |
return result; | |
} | |
} |
The Pipe
should be defined as impure with pure: false
because iterables like Map
doesn't change reference when using their methods. Be careful with large iterables though.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated version for angular 2 rc4 that works in ES5: