현재 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
경우 에만 렌더링 되고 싶습니다 locations.name
.
그래서 예를 들면,이있는 위치 name
의이 "Chicago O'Hare"
유효한 도시 이름이 될 것이며, 표시되어야합니다. 기호가있는 위치 name
의 "Chicago O'Hare A5C"
그러나, 그것은이있다보고 렌더링하지 않아야 A5C
을 유효하게 끝.
한 가지 접근 방식은 정규식을 사용 name
하여 다음을 통해 위치 필드 에 영숫자 하위 문자열 접미사가 있는지 여부에 따라 위치를 필터링하는 것입니다 .
const filteredLocations = this.props.locations.filter(location => {
return !location.name.match(/[A-Z0-9]+$/)
});
위의 논리는 기본적으로 다음과 같이 말합니다.
"위치 이름 끝에 영숫자 단어가없는 모든 위치 필터링" .
이를 renderLocation()
함수에 통합하는 방법은 다음과 같습니다.
renderLocation() {
/* Filter locations with name that doesn't have alphanumeric suffix word */
const filteredLocations = this.props.locations.filter(location => {
return !location.name.match(/[A-Z0-9]+$/)
});
/* Render only the filtered location with name Chicago O'Hare */
return filteredLocations.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>
);
});
}
도움이되는 희망
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다