Javascript 从Immutable.js中映射内部的列表中删除元素的最佳方法

Javascript 从Immutable.js中映射内部的列表中删除元素的最佳方法,javascript,immutable.js,Javascript,Immutable.js,我正在使用来加速我的React应用程序,以利用。我的一个数据结构是Map(),该映射中的一个键的值是List()。我想知道的是,不知道我想从列表()中删除的项目的索引,删除它的最佳方法是什么?到目前为止,我已经提出了以下建议。这是最好(最有效)的方法吗 //this.graphs是一个Map(),它在键“metrics”下包含一个List() onRemoveMetric:函数(graphId、metricUUID){ var index=this.graphs.getIn([graphId,“

我正在使用来加速我的React应用程序,以利用。我的一个数据结构是
Map()
,该映射中的一个键的值是
List()
。我想知道的是,不知道我想从
列表()中删除的项目的索引,删除它的最佳方法是什么?到目前为止,我已经提出了以下建议。这是最好(最有效)的方法吗

//this.graphs是一个Map(),它在键“metrics”下包含一个List()
onRemoveMetric:函数(graphId、metricUUID){
var index=this.graphs.getIn([graphId,“metrics”]).findIndex(函数(metric){
返回metric.get(“uuid”)==metricUUID;
});
this.graphs=this.graphs.deleteIn([graphdId,“metrics”,index]);
}
(我已经考虑将
List()
移动到
Map()
本身,因为列表中的每个元素都有一个UUID,但是,我还没有到这一步。)

您可以使用:

从性能的角度来看,切换到映射可能更有效,因为此代码(如您的代码)必须迭代列表中的元素。

按照@YakirNa的建议使用,如下所示

ES6:

ES5:


当然比我的简洁多了!是的,你和我都有解决方案,我认为如果不切换到
Map
,就无法提高效率。你现在可以使用,而不是重复
this.graphs.getIn([graphId,“metrics”])
// 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]);
}
onRemoveMetric: function(graphId, metricUUID) {
  this.graphs = this.graphs.setIn([graphId, "metrics"],
    this.graphs.getIn([graphId, "metrics"]).filter(function(metric) {
      return metric.get("uuid") !== metricUUID;
    })
  )
}
  onRemoveMetric(graphId, metricUUID) {
    this.graphs = this.graphs.updateIn([graphId, 'metrics'],
      (metrics) => metrics.filter(
        (metric) => metric.get('uuid') !== metricUUID
      )
    );
  }
  onRemoveMetric: function(graphId, metricUUID) {
    this.graphs = this.graphs.updateIn([graphId, "metrics"], function(metrics) {
      return metrics.filter(function(metric) {
        return metric.get("uuid") !== metricUUID;
      });
    });
  }