情景
开发中遇到的问题,Immutable.js数据很复杂,List嵌套Map,Map又有一个bList的值,bList是由bMap组成的,要过滤出bMap下某一value是否包含所键入的string。
大致简化模型如下:



相关的mock数据和keynote可以在百度云上查看,密码:xxge
。
解决方法
filteInputChange = (event) => { const key = event.target.value; const after = this.props.data.filter( e => e.get('students') .find( content => content.get('name').includes(key), ), ).map( e => e.update( v => v.set( 'students', v.get('students') .filter( content => content.get('name').includes(key), ), ), ), ); this.setState({ data: after });}
- 首先筛选出符合条件的section
- 再更新section下的students(
array
)
这里的update方法详细见官方介绍
If no key is provided, then the
updater
function return value is returned as well.
const aMap = Map({ key: 'value' })const result = aMap.update(aMap => aMap.get('key'))// "value"