ImmutableJS - 过滤嵌套的 Map{List(Map{...})}

ImmutableJS - filtering nested Map{List(Map{...})}

给定以下数据结构。

let hall = Map({
  tables: Map({
    t1: Map({
      playlist: List(
        Map({
          songid: 'target'
        })
      )
    }),
    t2: Map({
      playlist: List(
        Map({
          songid: 'not me'
        })
      )
    })
  })
});

如何遍历每个 table (t1, t2, ...) 并使用 songid === 'target' 删除列表中的项目,以便我最终得到:

let expected_hall = Map({
  tables: Map({
    t1: Map({
      playlist: List()
    }),
    t2: Map({
      playlist: List(
        Map({
          songid: 'not me'
        })
      )
    })
  })
});

已尝试以下方法无济于事:

let res = hall;
hall.get('tables').entrySeq().forEach(e => {
  res = res.updateIn(['tables', e[0], 'playlist'], list => list.filter(songinfo => songinfo.songid === 'target'));
});

// or using  hall.get('tables').map(...)

感谢您提供的所有帮助。

List#filter

的文档

Returns a new List with only the values for which the predicate function returns true.

所以这里我们必须 return 列出 songid 不是 target

let res = hall;
hall.get('tables').keySeq().forEach(e => {
  res = res.updateIn(['tables', e, 'playlist'], list => list.filter(info => info.get("songid") !== "target"));
});

let hall = Map({
  tables: Map({
    t1: Map({
      playlist: List.of(
        Map({
          songid: 'target'
        })
      )
    }),
    t2: Map({
      playlist: List.of(
        Map({
          songid: 'not me'
        })
      )
    })
  })
});

你使用List的方式,应该是List.of(...) 或者List([])

我会这样做:

const hall2 = hall.update("tables", tables => (
  tables.map(table => (
    table.update("playlist", playlist => playlist.filter(p => p.get("songid")!=="not me"))
  ))
));

您更新 tables 属性,遍历每个 table,更新播放列表 属性 并在不 [=30] 时按每个项目过滤播放列表=]