我在用Facebook 的 Immutable.js http://facebook.github.io/immutable-js/加速我的 React 应用程序以利用PureRender混合 https://facebook.github.io/react/docs/pure-render-mixin.html。我的数据结构之一是Map()
该地图中的键之一有List<Map>()
作为它的价值。我想知道的是,不知道我想从列表中删除的项目的索引List()
,删除它的最佳方法是什么?到目前为止,我已经提出了以下内容。这是最好的(最有效的)方法吗?
// this.graphs is a Map() which contains a List<Map>() under the key "metrics"
onRemoveMetric: function(graphId, metricUUID) {
var index = this.graphs.getIn([graphId, "metrics"]).findIndex(function(metric) {
return metric.get("uuid") === metricUUID;
});
this.graphs = this.graphs.deleteIn([graphdId, "metrics", index]);
}
(我考虑过移动List<Map>()
to a Map()
本身,因为列表中的每个元素都有一个 UUID,但是,我还没有到那一步。)
你可以使用Map.filter http://facebook.github.io/immutable-js/docs/#/Map/filter:
onRemoveMetric: function(graphId, metricUUID) {
this.graphs = this.graphs.setIn([graphId, "metrics"],
this.graphs.getIn([graphId, "metrics"]).filter(function(metric) {
return metric.get("uuid") !== metricUUID;
})
)
}
从性能的角度来看,切换到 Map 可能会更有效,因为此代码(如您的代码)必须迭代列表中的元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)