🐣

Immutable模糊搜索

情景

开发中遇到的问题,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 });
}
  1. 首先筛选出符合条件的section
  2. 再更新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"

在我们一生中,命运赐予我们每个人三个导师,三个朋友,三名敌人,三个挚爱。但这十二人总是不以真面目示人,总要等到我们爱上他们、离开他们、或与他们对抗时,才能知道他们是其中哪种角色。