Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- npm start
- package.json
- legacy-peer
- vscode git clone
- node설치
- 정적객체
- 그누보드반응형
- Git clone
- maxwidth
- fontawesome
- npm install 문제
- minwidth
- node 오류
- 이미지반응형
- googleicon
- 동적객체
- 단어단위로떨어지기
- slickslider
- 웹아이콘
- npm install
- git lab clone
- 글자들여쓰기
- MediaQuery
- 플러그인
- XEIcon
- owlcarousel
- 의존성문제
- 아이콘사용법
- react npm install
- window 정책변경
Archives
- Today
- Total
어쩌다 알게 된 ƪ(•̃͡•̃͡ ƪ
🟦 [React] 검색 기능 구현해보기 본문
728x90
구현 하려는 동작
=> 검색어를 받아와서 해당 탭의 데이터와 검색어가 일치하는 값을 출력
문제점
=> 일단 해당 값을 출력하는건 되었는데, 그 값이 없는 배열들이 빈 배열로 출력되고 있음
해야할 것
=> 빈 배열 없애기
const handleSearch = () => {
const keyword = searchInpRef.current.value.toLowerCase(); // 검색어 값
if (!searchInpRef.current || keyword.trim() === "") return;
setSearchTerm(keyword);
const filteredData = extractedValues[activeTab].map(tab =>
tab.filter(data =>
JSON.stringify(data).toLowerCase().includes(keyword)
)
)
console.log("---------------------")
console.log(searchInpRef.current.value)
console.log(filteredData)
console.log("---------------------")
}

📚 수정코드 📚
const handleSearch = () => {
const keyword = searchInpRef.current.value.toLowerCase();
if (!searchInpRef.current || keyword.trim() === "") return;
setSearchTerm(keyword);
const filteredData = extractedValues[activeTab].map(tab =>{
const filteredTab = tab.filter(data =>
JSON.stringify(data).toLowerCase().includes(keyword)
);
return filteredTab.length > 0 ? filteredTab : null; // 빈배열은 나오지 않게
}).filter(tab => tab !== null); // null 값은 제외하고 필터링
console.log("---------------------")
console.log(searchInpRef.current.value)
console.log(filteredData)
console.log("---------------------")
}
=> map() 내부에서 filter()로 데이터를 필터링한 후, 빈 배열이 나올 경우 null을 반환 == 빈 배열을 대신하는 값

일단 여기까지는 내가 원하는대로 됨!
근데 이제.. 저 중첩배열안에 값이 서브 말고 다른 값들도 가져와야되는데.. 😢 어려버..

🟦 [React] 검색 기능 구현해보기 2
이전 로그👇 검색어를 받아와서 해당 탭의 데이터와 검색어가 일치하는 값을 출력 문제점=> 일단 해당 값을 출력하는건 되었는데, 그 값이 없는 배열들이 빈 배열로 출력되고 있음 해야" data-og
dazzle-bini.tistory.com
728x90
'개발 > 🟦 React' 카테고리의 다른 글
| 🟦 [React] console.log가 setFilteredResult: function () { [native code] } 이렇게 뜰 때 (0) | 2025.03.24 |
|---|---|
| 🟦 [React] 검색 기능 구현해보기 2 (0) | 2025.03.24 |
| 🟦 [React] Uncaught TypeError: Cannot read properties of undefined (reading 'filter') (0) | 2025.03.23 |
| 🟦 [React] onClick={함수}, {함수()}, {() => 함수}, {() => 함수()} 차이 (0) | 2025.03.20 |
| 🟦 [React] 텍스트 불러올 때 <br /> 사용하기 => <React.Fragment> 이용 (0) | 2025.03.20 |
Comments
