[英]React-Native Javascript Search Filter Function
I am trying to search for these ingredients in this array and add the value to the data
to display it but it is not working.我试图在这个数组中搜索这些成分并将值添加到data
以显示它,但它不起作用。 I am very new to javascript so i'd really appreciate if anyone is kind enough to help me out .This is what I have done:我对 javascript 很陌生,所以如果有人能帮助我,我真的很感激。这就是我所做的:
const Search = () => {
const ingridients = [ ["paprika", "parsley"], ["steak", "ground beef"],["milk", "eggs", "cheese"] ]
const [input, setInput] = useState();
const categorize = (term) => {
setInput(ter )
let data = []
if (term) {
const newData = ingridients.filter(() => {
if (RegExp(searchTerm, "gim").exec())
return RegExp(searchTerm, "gim").exec()
})
} else {
data = []
}
}
return(
<TextInput onChange={(text)=> categorize(text)} value={input}/>
)
}
Do you mean something like this?你的意思是这样的吗?
import React, { useState } from 'react';
import { Text, TextInput, View } from 'react-native';
export default function App() {
const ingridients = [
'paprika',
'parsley',
'steak',
'ground beef',
'milk',
'eggs',
'cheese',
];
const [input, setInput] = useState();
const [filteredData, setFilteredData] = useState([]);
const filter = (term) => {
setInput(term);
if(/\d/.test(term) || /[a-zA-Z]/.test(term)) {
setFilteredData(ingridients.filter(ingridient => ingridient.includes(term)));
}
else {
setFilteredData([]);
}
};
return (
<React.Fragment>
<TextInput
onChangeText={(text) => filter(text)}
value={input}
style={{ backgroundColor: 'grey' }}
/>
<View style={{ padding: 40 }}>
{filteredData.map(ingridient => (
<Text style={{ color: 'black' }}>{ingridient}</Text>
))}
</View>
</React.Fragment>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.