【发布时间】:2017-08-05 09:13:50
【问题描述】:
一个 React 组件被传递一个 state 属性,它是一个对象的对象:
{
things: {
1: {
name: 'fridge',
attributes: []
},
2: {
name: 'ashtray',
attributes: []
}
}
}
它也被传递(作为路由器参数)name。我希望组件通过比较 name 值在 things 对象中找到匹配的对象。
为此,我使用filter 方法:
Object.keys(this.props.things).filter((id) => {
if (this.props.things[id].name === this.props.match.params.name) console.log('found!');
return (this.props.things[id].name === this.props.match.params.name);
});
但是这会返回undefined。我知道条件有效是因为我的测试行(console.log 行)将found 记录到控制台。为什么filter 方法返回undefined?
【问题讨论】:
-
你能显示你使用这个sn-p的整个代码吗?
标签: javascript reactjs filter