I have some JSON (see end) that contains two values. I need the search to filter based on either value. I documented every part of the search/display of code
Here is my useState variables:
const [dataBackup, setdataBackup] = useState([]) const [dataSource, setdataSource] = useState([])
Here is my FlatList:
<SectionList
contentContainerStyle={{ paddingHorizontal: 10}}
stickySectionHeadersEnabled={false}
sections={dataSource}
keyExtractor={(item, index) => {
return index.toString();
}}
renderSectionHeader={({ section }) => (
<>
<Text style={styles.sectionHeader}>{section.title}</Text>
{section.data ? (
<OptimizedFlatList
horizontal
data={section.data}
keyExtractor={(item, index) => {
return index.toString();
}}
renderItem={({ item }) => <ListItem item={item} navigation={navigation} />}
showsHorizontalScrollIndicator={false}
/>
) : null}
</>
)}
renderItem={({ item, section, navigation }) => {
if (section.data) {
return null;
}
return <ListItem item={item} navigation={navigation}/>;
}}
/>
Here is the NOT WORKING filtering logic:
filterList = (text) => {
for (let i = 0; i < dataBackup.length; i++) {
const t = dataBackup[i];
newData = t["data"].filter((item) => {
const itemData = item["name"].toLowerCase();
const textData = text.toLowerCase();
setdataSource(itemData.indexOf(textData) > -1)
});
}
}
Here is what the data looks like:
[{
title: 'Leg',
data:[
{
"bodyPart": "lower legs",
"equipment": "assisted",
"gifUrl": "http://d205bpvrqc9yn1.cloudfront.net/1708.gif",
"id": "1708",
"name": "assisted lying calves stretch",
"target": "calves",
"broad_target": "legs",
"ppl": "legs"
},
{
"bodyPart": "lower legs",
"equipment": "smith machine",
"gifUrl": "http://d205bpvrqc9yn1.cloudfront.net/1396.gif",
"id": "1396",
"name": "smith toe raise",
"target": "calves",
"broad_target": "legs",
"ppl": "legs"
}
]
},
{
title: 'Back',
data:[
{
"bodyPart": "Back",
"equipment": "assisted",
"gifUrl": "http://d205bpvrqc9yn1.cloudfront.net/1708.gif",
"id": "1708",
"name": "Back1",
"target": "Back",
"broad_target": "Back",
"ppl": "Pull"
},
{
"bodyPart": "Back",
"equipment": "smith machine",
"gifUrl": "http://d205bpvrqc9yn1.cloudfront.net/1396.gif",
"id": "1396",
"name": "Back2",
"target": "Back",
"broad_target": "Back",
"ppl": "Pull"
}
]
}]
I want it to filter by Title OR by name