【问题标题】:How to get next element in Immutable List when use .map?使用.map时如何获取不可变列表中的下一个元素?
【发布时间】:2019-01-31 12:37:45
【问题描述】:

我有 1 个这样的不可变列表

const list = new List([
new Map({ title: 'first', complete: true }),
new Map({ title: 'second',complete: false }),
])

当我使用时

list.map((value,index) => {
//HOW TO GET VALUE OF NEXT ELEMENT IN HERE ?
})

【问题讨论】:

    标签: javascript reactjs immutable.js


    【解决方案1】:

    你可以这样做

    list.map((value,index) => {
      const next = list.get(index + 1);
      // next will be undefined when the value is last one in the list (and index + 1) is out of bounds because of that
    })
    

    只是为了澄清,这个例子使用了 Immutable List 是 Collection.Indexed 的后代的事实,它具有get(index) method

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      相关资源
      最近更新 更多