不可变 JS - 在嵌套映射中获取值
Immutable JS - get a value inside a nested map
我正在使用 immutableJS,React/Redux 我有这张地图,我需要获取 actions
的值,所以它会像 -> allRetrospectivesMap
-> id
-> current_stage
-> actions
我有这段代码,它可以工作,但它超级难看,有没有更好的方法?
class UserActionItems extends Component {
render() {
const { retrospectives } = this.props
const actions = flatten(
Object.keys(immutableHelpers.toJS(retrospectives))
.map(key => retrospectives.get(key))
.map(retro => immutableHelpers.toJS(retro.getIn(['current_stage', 'actions'])))
).value()
return (
<div>
<ActionsList
actions={actions[0]}
users={[]}
/>
</div>
)
}
}
const mapStateToProps = ({ allRetrospectivesMap }) => ({
retrospectives: allRetrospectivesMap
})
谢谢!!! :)
您可以使用来自不可变 js 的 getIn()
方法来做到这一点。
const id = getId() // you've id from somewhere
const actions= state.getIn(['allRetrospectivesMap', id, 'current_stage', 'actions']); // Note: state is your immutable data.
了解更多信息 here
我正在使用 immutableJS,React/Redux 我有这张地图,我需要获取 actions
的值,所以它会像 -> allRetrospectivesMap
-> id
-> current_stage
-> actions
我有这段代码,它可以工作,但它超级难看,有没有更好的方法?
class UserActionItems extends Component {
render() {
const { retrospectives } = this.props
const actions = flatten(
Object.keys(immutableHelpers.toJS(retrospectives))
.map(key => retrospectives.get(key))
.map(retro => immutableHelpers.toJS(retro.getIn(['current_stage', 'actions'])))
).value()
return (
<div>
<ActionsList
actions={actions[0]}
users={[]}
/>
</div>
)
}
}
const mapStateToProps = ({ allRetrospectivesMap }) => ({
retrospectives: allRetrospectivesMap
})
谢谢!!! :)
您可以使用来自不可变 js 的 getIn()
方法来做到这一点。
const id = getId() // you've id from somewhere
const actions= state.getIn(['allRetrospectivesMap', id, 'current_stage', 'actions']); // Note: state is your immutable data.
了解更多信息 here