我正在使用React Native的SectionList。 SectionList中的数据看起来像这样
library(reader)
library(stringr)
df2 <- read_csv("
CTsite ,sequence_annotation
OCCAJ01 ,Lt
OCCAJ01 ,20
OCCAJ04 ,Mt
OCCAJ04 ,40",skip=1)
df1 <- read_csv("
CTsite ,AnX ,PoleX
OCCAJ01 ,720 ,2592
OCCAJ04 ,640 ,3264",skip=1)
df2 %>% inner_join(df1,by="CTsite") %>%
mutate(xres=ifelse(str_detect(sequence_annotation,"\\D"),AnX,PoleX))
## A tibble: 4 x 5
# CTsite sequence_annotation AnX PoleX a
# <chr> <chr> <int> <int> <int>
#1 OCCAJ01 Lt 720 2592 720
#2 OCCAJ01 20 720 2592 2592
#3 OCCAJ04 Mt 640 3264 640
#4 OCCAJ04 40 640 3264 3264
我有一个文本输入,尝试通过它过滤掉SectionList中的内容。我尝试使用data: [
{
title: "Asia",
data: ["Taj Mahal", "Great Wall of China", "Petra"]
},
{
title: "South America",
data: ["Machu Picchu", "Christ the Redeemer", "Chichen Itza"]
},
{
title: "Europe",
data: ["Roman Colosseum"]
}
]
,但似乎不起作用。它返回我全部数据,而没有任何过滤。因此,我尝试了Array.filter()
。现在,即使有一项匹配,也会过滤该部分中的所有数据项。 Array.some()
可能会出现这种情况。但是我很困惑为什么Array.some()
在我的情况下不起作用。
我的SectionList看起来像这样,
Array.filter()
如果您想在线玩Expo Playground,请点击这里。
答案 0 :(得分:5)
filter
将创建一个包含所有返回真实值的条目的新数组。您的第二个过滤器将始终至少返回一个空数组,这是事实,因此您可以在最终结果中获得所有节。
您可以尝试将reduce
和filter
组合使用:
this.state.data.reduce((result, sectionData) => {
const { title, data } = sectionData;
const filteredData = data.filter(
element => element.includes(this.state.searchTerm)
);
if (filteredData.length !== 0) {
result.push({
title,
data: filteredData
});
}
return result;
}, [])