【发布时间】:2019-04-02 03:12:08
【问题描述】:
我目前正在使用map() 函数呈现从 API 返回的数据列表,如下所示:
renderLocation() {
return this.props.locations.map(location => {
return (
<div key={location.id}>
<div className="location">
<h1>
{location.name}
{location.airport_code}
</h1>
<div className="location-secondary-info">
<span>
<i className="material-icons">airplanemode_active</i>
{location.description}
</span>
</div>
</div>
</div>
);
});
}
我现在想过滤渲染,以便渲染的唯一locations 是那些具有正确name 字段值的那些。我的 API 数据如下所示:
如果locations.name 是实际有效的城市名称,我只希望呈现locations。
因此,例如,name 或 "Chicago O'Hare" 的位置将是有效的城市名称,应该显示。但是,不应呈现具有 name 或 "Chicago O'Hare A5C" 的位置,因为它的末尾有 A5C,这使其无效。
【问题讨论】:
标签: arrays reactjs ecmascript-6 filter