简体   繁体   English

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

[英]ImmutableJS - filtering nested Map{List(Map{…})}

Given the following data structure. 给定以下数据结构。

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

How do I iterate over each table (t1, t2, ...) and remove the item in the list with songid === 'target' so that I end up with: 如何遍历每个表(t1,t2,...)并使用songid === 'target'删除列表中的项目,所以我最终得到:

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

Tried the following to no avail: 尝试以下操作无济于事:

 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(...) 

Appreciate for all the help given. 感谢所有提供的帮助。

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

The way you've used List, it should be List.of(...) or List([]) 使用List的方式应该是List.of(...)或List([])

I would do it like this: 我会这样做:

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

You update the tables property, iterate over each table, update the playlist property and filter the playlist by each item on not being "not me" 您更新表属性,遍历每个表,更新播放列表属性,并根据不是“不是我”的每个项目过滤播放列表

Docs of List#filter List#filter的文档

Returns a new List with only the values for which the predicate function returns true. 返回一个新List,其中仅包含谓词函数返回true的值。

So here we have to return which list whom songid IS NOT target 因此,在这里我们必须返回 target 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")); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM