Maps are Iterable

Maps in Immutable.js are iterable, meaning that you can map, filter, reduce, etc. each key-value pair in the map.

  1. let features = Immutable.Map<string, boolean>({
  2. 'send-links': true,
  3. 'send-files': true,
  4. 'local-storage': true,
  5. 'mirror-notifications': false,
  6. 'api-access': false
  7. });
  8. let myFeatures = features.reduce((providedFeatures, provided, feature) => {
  9. if(provided)
  10. providedFeatures.push(feature);
  11. return providedFeatures;
  12. }, []);
  13. console.log(myFeatures); // [ 'send-links', 'send-files', 'local-storage' ]
  1. const mapMap = Immutable.Map({ a: 0, b: 1, c: 2 });
  2. mapMap.map(i => i * 30);
  3. const mapFilter = Immutable.Map({ a: 0, b: 1, c: 2 });
  4. mapFilter.filter(i => i % 2);
  5. const mapReduce = Immutable.Map({ a: 10, b: 20, c: 30 });
  6. mapReduce.reduce((acc, i) => acc + i, 0);

原文: https://angular-2-training-book.rangle.io/handout/immutable/immutable-js/nested-objects/maps_are_iterable.html