鍍金池/ 問答/HTML/ seamless-immutable的使用問題

seamless-immutable的使用問題

現在有這么一段數據

let a = [{list:[1,2,3]},{list:[4,5,6]}];
let aIm = immutable(a);

我想把list數組中的每一項都加1,應該怎么寫?

回答
編輯回答
默念

可以這樣做,用兩層reduce來實現:


(() => {
  let a = [{list: [1, 2, 3]}, {list: [4, 5, 6]}]
  let aIm = Immutable(a)

  aIm = aIm.reduce((aIm, item, index) =>
    aIm.updateIn([index, 'list'], add), aIm)

  function add (arr) {
    return arr.reduce((arr, item, index) =>
      arr.updateIn([index], plus), arr)
  }

  function plus (x) {
    return x + 1
  }

  console.log(aIm)
})()
2017年6月10日 04:30
編輯回答
哎呦喂
a.map(x => {
    return x.list.map(y => y+1)
})
2018年5月7日 14:23
編輯回答
安若晴
let list = Immutable.fromJS([{list:[1,2,3]},{list:[4,5,6]}])
list.map(item => item.updateIn(['list'], list => list.map(n => ++n))).toJS()

clipboard.png

2018年4月11日 10:25