2017-09-04 52 views
1

我使用Immutablejs更新所有elments陣列中的

更新數組中的所有元素

的JSON看起來是這樣的:

imState = Immutable.fromJS({ 
    app: { 
    line: { 
     name: "Bar Chart", 
     series: [ 
     { 
      name: "Series1x", 
      color: "#82ca9d" 
     }, 
     { 
      name: "Series2x", 
      color: "#2239ca" 
     }, 
     { 
      name: "Series3x", 
      color: "#c2a5ca" 
     } 
     ] 
    } 
    } 
}) 

,我只是想在遍歷所有的系列元素並將顏色更改爲固定顏色「#1bf115」。

我猜你會使用更新函數。這個函數沒有API文檔,所以我一直在做很多試驗的一個錯誤。

我試圖用這樣的:

imState = imState.update(
    ['app', 'line', 'series'], 
    series => series.map(s => s.update('color', color => "#1bf115")) 
) 

但是我在series.map得到一個未定義的錯誤。

爲什麼這是錯的?

回答

1

因爲您提供的是深層嵌套的路徑,而不是update,請使用updateIn

imState = imState.updateIn(
    ['app', 'line', 'series'], 
    series => series.map(s => s.update('color', color => "#1bf115")) 
)