jcalfee314 jcalfee314 - 3 months ago 16
Javascript Question

Searching an immutable JS Map

Can you please suggest the most efficient way to search Immutable.Map's values? I'm looking to return the first match.

https://facebook.github.io/immutable-js/

I believe I should get a

map.valueSeq()
and go from there. I'm trying to do something like this:

Immutable= require("immutable")
var keys = Immutable.Map()
k=keys.set(1,2)
var result = null
k.valueSeq().map(
function(value) {
if(value == 2)
result = value
}
)
return result


I would like to stick with the Map data structure, it is used elsewhere in the code.

Answer

You can use .find:

var Immutable = require("immutable");
var map = Immutable.Map();
var m = map.set(1,2);
return m.find(function(val) {
    return value == 2;
});

You can use m.valueSeq().find as well.