Immutablejs flatMap only preservers first element of inner map?

23 Views Asked by At

Context: drawing a node graph, I have a Immutable.Map<MapPointId, Immutable.Map<MapPointId, number>> object to store the edges between nodes (the number is just a 'cost' for pathfinding)

As part of the visualization, I create a flattened array of react nodes using flatMap on the outer Map, and maps on the internal Maps like so:

const mapConnections = props.GameMap.MapEdges
    .flatMap((edgesFromPoint, pointId) =>
        edgesFromPoint.map((edgeCost, otherPointId) => {...react component...}
        )
    )
    .valueSeq().toArray();

but for some reason I can't figure out, this results in the resulting array containing only one element for each inner map.

I can achieve what I want by the following changes:

const mapConnections = props.GameMap.MapEdges
    .map((edgesFromPoint, pointId) =>
        edgesFromPoint.map((edgeCost, otherPointId) => {...react component...}
        ).valueSeq()
    )
    .valueSeq().flatMap(s => s).toArray();

but I can't figure out why the first one doesn't work, and also, what would be an actual good solution, because the second case, using a useless s => s lambda seems unnecessarily complicated.

0

There are 0 best solutions below