使用immutable要如何更改数据结构嵌套较深的对象

#1
const testInitialState = Immutable.fromJS({
    selectedIndex: -1,
    componentsConfig: [
        {
            name: 'HS_Form',
            key: 3,
            config: {
                inputs: {
                    value: [
                        {
                            label: '输入框一',
                            type: 'text'
                        },
                        {
                            label: '输入框二',
                            type: 'email'
                        },
                        {
                            label: '输入框三',
                            type: 'tel'
                        },
                        {
                            label: '请输入选择项',
                            type: 'radio',
                            value: ['男', '女']
                        }
                    ]
                }
            }
        }
    ]
})

比如我要修改componentsConfig里面key为3的对象的config属性的值,要怎么改呢,有点懵逼:cry:

#2
testInitialState.update( 'componentsConfig' , $tmpList=>{  //一层的操作可以用update,层级深的没办法只能updateIn
    let index = $tmpList.findIndex( $obj=>{ return $obj.get( 'key' ) === 3 ; } ) ;
    if( index === -1 ){     // 没找见,findIndex 返回-1
        return $tmpList ;
    }else{
        return $tmpList.update( index , $obj=>{
               return $obj.set( 'new_key' , 5 ) ;
        } ) ;
    }
} )

思路就是,找到componentsConfig数组中你要修改的元素,然后调用Immutable的修改数据结构的API(这里是updateInset之类的)。

官网API研究下:immutable-js docs。中文api资料比较少,还是老老实实啃吧。

#3

解决了,多谢大佬:grinning: