给定以下数据结构。
let hall = Map({
tables: Map({
t1: Map({
playlist: List(
Map({
songid: 'target'
})
)
}),
t2: Map({
playlist: List(
Map({
songid: 'not me'
})
)
})
})
});
如何迭代每个表(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(...)
感谢所提供的所有帮助。
最佳答案
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"))
))
));
您更新表格属性,迭代每个表格,更新播放列表属性并按每个项目过滤播放列表,以免“不是我”
关于javascript - ImmutableJS - 过滤嵌套 Map{List(Map{...})},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45631408/