Javascript 过滤本机节列表中的数据

Javascript 过滤本机节列表中的数据,javascript,reactjs,react-native,react-native-sectionlist,Javascript,Reactjs,React Native,React Native Sectionlist,我正在使用React Native的分区列表。SectionList中的数据如下所示 data: [ { title: "Asia", data: ["Taj Mahal", "Great Wall of China", "Petra"] }, { title: "South America", data: ["Machu Picchu", "Christ the Redeemer", "Chichen Itza"]

我正在使用React Native的分区列表。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"]
    }
  ]
我有一个文本输入,我试图用它过滤掉SectionList中的内容。我尝试使用
Array.filter()
,但似乎不起作用。它返回给我整个数据,没有任何过滤。因此,我尝试了
Array.some()
。现在,该部分中的所有数据项都将被过滤,即使有一项与之匹配。此行为应在
数组.some()中出现。但是我不明白为什么
Array.filter()
在我的例子中不起作用

我的分区列表是这样的

<SectionList 
      sections={this.state.data.filter(sectionData => {
        sectionData = sectionData.data;
        return sectionData.filter(data => {
          return data.includes(this.state.searchTerm);
        })
      })}
      renderSectionHeader={({ section: { title } }) => ( <Text style={{ fontWeight: "bold" }}>{title}</Text> )}
      renderItem={({ item }) => ( <Text style={styles.listItem}>{item}</Text>)}
      keyExtractor={item => item}
    />
{
sectionData=sectionData.data;
返回sectionData.filter(数据=>{
返回数据.includes(this.state.searchTerm);
})
})}
renderSectionHeader={({section:{title}}})=>({title}})
renderItem={({item})=>({item})}
keyExtractor={item=>item}
/>

这里有一个链接,供您在线使用。

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;
}, [])

多谢。使用减速机实际上解决了我的问题。