检查一个字符串是否包含另一个字符串 React Native [英] Check if a string contains another string React Native
问题描述
我正在尝试像这样过滤数据数组:
让数据=[{"批准": "TPED",制造商":切斯特菲尔德"},{"批准": "BV",制造商":Faber"}]让批准变量 = "TP"让filteredData = data.filter(x => x.approval.includes(approvalVariable))console.log(filteredData)
所以如果approvalVariable是TP",我希望新数组是:
<预><代码>[{"批准": "TPED",制造商":切斯特菲尔德"},]当我这样做时,它可以工作:
let filtersData = data.filter(x => x.approval == ApprovalVariable)
但是当我尝试时:
x.approval.includes(approvalVariable)
我得到一个错误,x.approval.includes 不是一个对象
我曾经用 .includes() 让它工作过,但现在出了点问题.
任何帮助将不胜感激.
componentWillMount() {this.fetchData();}fetchData = async() =>{var fireBaseResponse = firebase.database().ref();fireBaseResponse.once('value').then(snapshot => {让数据1 = [];让批准 = this.props.navigation.state.params.approval让评论 = this.props.navigation.state.params.comments让 designStandard = this.props.navigation.state.params.designStandard让直径 = this.props.navigation.state.params.diameter让 h2Compatible = this.props.navigation.state.params.h2compatible让入口线程 = this.props.navigation.state.params.inletThread让制造商 = this.props.navigation.state.params.manufacturer让规范编号 = this.props.navigation.state.params.specificationNumber让 testPressure = this.props.navigation.state.params.testPressure让 waterCapacity = this.props.navigation.state.params.waterCapacity让workingPressure = this.props.navigation.state.params.workingPressure快照.forEach(项目=>{const temp = item.val();data1.push(temp);返回假;});////////过滤方法////////如果(批准 == ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.approval.includes(approval))}如果(水容量 == ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.waterCapacity == waterCapacity)}如果(设计标准== ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.designStandard == designStandard)}如果(直径==''){控制台日志(好")}别的 {data1 = data1.filter(x => x.diameter == 直径)}如果(入口线程 == ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.inletThread == inletThread)}如果(工作压力==''){控制台日志(好")}别的 {data1 = data1.filter(x => x.workingPressure == workingPressure)}如果(评论 == ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.comments == 评论)}如果(制造商 == ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.manufacturer == 制造商)}如果(测试压力 == ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.testPressure == testPressure)}如果(规范编号 == ''){控制台日志(好")}别的 {data1 = data1.filter(x => x.specificationNumber == specificationNumber)}if(h2Compatible == '') {控制台日志(好")}别的 {data1 = data1.filter(x => x.h2Compatible == h2Compatible)}/////////////////////过滤方法//////////////////this.setState({data: data1});});}使成为(){var {navigate} = this.props.navigation;让 {params} = this.props.navigation.state;返回(<视图容器><滚动视图><平面列表数据 = {this.state.data}keyExtractor = {(x, i) =>一世}renderItem ={({item}) =><文本样式 = {styles.itemText}>批准:{item.approval} |制造商:{item.manufacturer} |规格编号:{item.specificationNumber} |H2 兼容:{item.h2Compatible} |直径:{item.diameter} |水容量:{item.waterCapacity} |入口线程:{item.inletThread}{"\n"}</文本>}/></滚动视图><查看样式={styles.footer}><TouchableOpacity style ={styles.footerButton} onPress = { () =>导航(ValveSearchScreen")}><文本样式 ={styles.footerButtonText}>SEARCH</Text></TouchableOpacity></查看></ViewContainer>)}}
问题是它在名为 includes 的数组对象中搜索属性.显然它找不到它,所以它警告我该财产不存在.为了解决这个问题,我将行更改为
let filtersData = data.filter(x => String(x.approval).includes(approvalVariable));
我希望这会在未来帮助其他人,而您不会像我一样在没有帮助的情况下花一周时间尝试解决问题.
I am trying to filter an array of data like so:
let data =
[
{
"approval": "TPED",
"manufacturer": "Chesterfield"
},
{
"approval": "BV",
"manufacturer": "Faber"
}
]
let approvalVariable = "TP"
let filteredData = data.filter(x => x.approval.includes(approvalVariable))
console.log(filteredData)
So if approvalVariable is "TP", I want the new array to be:
[
{
"approval": "TPED",
"manufacturer": "Chesterfield"
},
]
I have it working when I do:
let filteredData = data.filter(x => x.approval == approvalVariable)
But when I try:
x.approval.includes(approvalVariable)
I get an error that x.approval.includes is not an object
I had it working at one point with .includes() but something is going wrong now.
Any help would be greatly appreciated.
componentWillMount() {
this.fetchData();
}
fetchData = async () => {
var fireBaseResponse = firebase.database().ref();
fireBaseResponse.once('value').then(snapshot => {
let data1 = [];
let approval = this.props.navigation.state.params.approval
let comments = this.props.navigation.state.params.comments
let designStandard = this.props.navigation.state.params.designStandard
let diameter = this.props.navigation.state.params.diameter
let h2Compatible = this.props.navigation.state.params.h2compatible
let inletThread = this.props.navigation.state.params.inletThread
let manufacturer = this.props.navigation.state.params.manufacturer
let specificationNumber = this.props.navigation.state.params.specificationNumber
let testPressure = this.props.navigation.state.params.testPressure
let waterCapacity = this.props.navigation.state.params.waterCapacity
let workingPressure = this.props.navigation.state.params.workingPressure
snapshot.forEach(item =>{
const temp = item.val();
data1.push(temp);
return false;
});
////////Filter Method/////////
if(approval == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.approval.includes(approval))
}
if(waterCapacity == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.waterCapacity == waterCapacity)
}
if(designStandard== '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.designStandard == designStandard)
}
if(diameter == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.diameter == diameter)
}
if(inletThread == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.inletThread == inletThread)
}
if(workingPressure == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.workingPressure == workingPressure)
}
if(comments == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.comments == comments)
}
if(manufacturer == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.manufacturer == manufacturer)
}
if(testPressure == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.testPressure == testPressure)
}
if(specificationNumber == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.specificationNumber == specificationNumber)
}
if(h2Compatible == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.h2Compatible == h2Compatible)
}
/////////////////////Filter Method//////////////////
this.setState({data: data1});
});
}
render(){
var {navigate} = this.props.navigation;
let {params} = this.props.navigation.state;
return(
<ViewContainer>
<ScrollView>
<FlatList
data = {this.state.data}
keyExtractor = {(x, i) => i}
renderItem ={({item}) =>
<Text style = {styles.itemText}>
Approval: {item.approval} | Manufacturer: {item.manufacturer} | Specification Number: {item.specificationNumber} |
H2 Compatible: {item.h2Compatible} | Diameter: {item.diameter} | Water Capacity: {item.waterCapacity} |
Inlet Thread: {item.inletThread}{"\n"}
</Text>
}
/>
</ScrollView>
<View style ={styles.footer}>
<TouchableOpacity style ={styles.footerButton} onPress = { () => navigate("ValveSearchScreen")}>
<Text style ={styles.footerButtonText}>SEARCH</Text>
</TouchableOpacity>
</View>
</ViewContainer>
)
}
}
The problem was that it was searching for a property within the array object called includes. Obviously it could not find it so it was giving me the warning that the property did not exist. To fix this I changed the line to
let filteredData = data.filter(x => String(x.approval).includes(approvalVariable));
I hope this helps somebody else out in the future and you don't spend a week trying the figure it out with no help like I did.
这篇关于检查一个字符串是否包含另一个字符串 React Native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!