javascript - 检查一个字符串是否包含另一个字符串 React Native

标签 javascript react-native

我正在尝试像这样过滤一组数据:

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)

所以如果 approvalVariable 是“TP”,我希望新数组是:

[
   {
    "approval": "TPED",
    "manufacturer": "Chesterfield"
   },
]

当我这样做的时候我让它工作:

let filteredData = 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 => {
     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>

    )
  }
}

最佳答案

问题在于它正在搜索名为 includes 的数组对象中的属性。显然它找不到它,所以它警告我该属性不存在。为了解决这个问题,我将行更改为

let filteredData = data.filter(x => String(x.approval).includes(approvalVariable));

我希望这对以后的其他人有所帮助,您不会像我一样在没有帮助的情况下花一个星期的时间尝试解决问题。

关于javascript - 检查一个字符串是否包含另一个字符串 React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085097/

相关文章:

javascript - React Native 中的 Redux Form,带有 Value 的 onChangeText 无法正常工作

javascript - 使用 jquery regex 函数显示匹配的字符串

javascript - 更新不起作用 mongodb、node.js

javascript - _react2.default.PropTypes.function 未定义

react-native - 排毒:检测显示的元素

android - 如何将 versionCode 转化为 native 代码 (android)

javascript - 如何将 js filereader 结果保存到变量中以供进一步使用?

javascript - window.open() 参数在 Edge 中不起作用

javascript - 过滤我的对象

android - 可以在 Android 和 iOS 应用程序中嵌入 3D View 吗?