Pierwszą rzeczą, aby pamiętać, że nie jesteś w rzeczywistości stworzenie mapy, tworzysz listę:
var result = [{'id': 2}, {'id': 4}];
var map = Immutable.fromJS(result);
Immutable.Map.isMap(map); // false
Immutable.List.isList(map); // true
Aby utworzyć mapę, można użyć argumentu reviver
w swoim zgłoszeniu toJS
(docs), ale z pewnością nie jest to najbardziej intuicyjny interfejs API, alternatywnie można zrobić coś takiego:
// lets use letters rather than numbers as numbers get coerced to strings anyway
var result = [{'id': 'a'}, {'id': 'b'}];
var map = Immutable.Map(result.reduce(function(previous, current) {
previous[ current.id ] = current;
return previous;
}, {}));
Immutable.Map.isMap(map); // true
Teraz mamy odpowiednią mapę Immutable.js który posiada metodę get
var item = Map.get('a'); // {id: 'a'}
Ach, miło, próbowałem tego, ale nie udało mi się, nie mam już po co. Ale to bardziej precyzyjne. Dzięki! – sspross
To również działa dla List. Dzięki. – Lebul