搜索一个不可变的JS Map

Searching an immutable JS Map

本文关键字:不可变 JS Map 一个 搜索      更新时间:2023-09-26

您能建议最有效的搜索Immutable的方法吗?地图的价值观?我希望返回第一场比赛。

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

我相信我应该得到一个map.valueSeq(),然后从那里开始。我想做这样的事情:

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

我想坚持使用Map数据结构,它在代码的其他地方使用。

您可以使用.find:

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

也可以用m.valueSeq().find

Immutable.Map.Find

例子
var map = Immutable.Map()
map = map.set(1,2) //set or add data to map
var criteria=2;
var finded=map.find(function(data,key) {
    return data===criteria;//return the first value of data that return true
}, null);//return null if not found data

参见:https://jsfiddle.net/fyr5nk4x/